题解 | #成绩排序#学习大佬思路
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
while True: try: num = int(input()) n = int(input()) dic = {} # 更新成绩:名字字典(相同成绩的名字合在一起作为一个值) for i in range(num): person = input().split() # 返回一个列表,列表里每个元素为str类型 if int(person[1]) in dic: # 成绩后面加名字,重复名字也没有关系 # 将值原来的字符串(名字:为列表类型)连接新的字符串(重复人的名字) dic[int(person[1])].append(person[0]) else: dic[int(person[1])] = [person[0]] # 新增为 成绩:[名字]这样的键值对 if n == 0: # 使用 sorted() 函数可以直接对字典排序, # 排序后生成的是‘只包含’字典‘键’的列表, # 默认按’键‘的升序排列 for i in sorted(dic, reverse=True): # 降序排序 for j in dic[i]: print(j + " " + str(i)) else: for i in sorted(dic): # 升序排序 for j in dic[i]: print(j + " " + str(i)) except: break