This is a personal memo.
· Nested list of given numbers and names ・ Extract the name of the person with the second smallest number in alphabetical order
▼sample input
5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
▼my answer
if __name__ == '__main__':
#Prepare an array to store the values
nameScores=[]
scores=[]
for _ in range(int(input())):
name = input()
score = float(input())
scores.append(score)
nameScore=[name, score]
nameScores.append(nameScore)
#Sort by name alphabetically
nameScores = sorted(nameScores)
#Find the second smallest value
secondlow = sorted(set(scores))[1]
#Extract the name with the minimum value
names=[]
for nameScore in nameScores:
if nameScore[1]==secondlow:
print(nameScore[0])
・ Array.append (value)
Add value to array
・ Sorted
Sort the array in ascending order (0th element in case of nesting)
Alphabet = a ~
Numerical value = 0 ~
Descending order and key elements can be specified as options.
・ Descending order: reverse = True
-Key specification: Specify with a lambda expression.
key = lambda val: val [index number]
key = lambda x: x [key name]
・ Set ()
Remove duplicate values
set type
Recommended Posts