You are viewing a single comment's thread. Return to all comments →
List comprehensions are so great!
if __name__ == '__main__':
for _ in range(int(input())):
name = input()
score = float(input())
b = [i for i in a if i != a]
c = [j for j in b if j == b]
c.sort(key=lambda x: x)
for i in range(len(c)):
list a is simply a list of all the input data. It is sorted so that the lowest score is in the first index.
list b uses a list comprehension to remove all instances of the lowest score from list a. The lowest remaining score in the resulting list (found in the first index since it will be the first value taken from the already-sorted list a) is the second-lowest score overall.
list c then uses another list comprehension to trim down list b to only those scores that are equal to that second-lowest overall score.
list c is now a nested list containing the scores and names of the students that will make up the output.
It is sorted alphabetically according to student name by using a lambda expression which indicates to use the contents of the second index of each object to be sorted ('name' in [score, name]) as the key for sorting.
Finally the names from list c are printed to satisfy the problem.