我是python的新手,我想将所有最高分数按降序排序。 (有一个字典存储每个学生的数据)目前它只按照输入数据的顺序打印每个学生的最高分数:
Maximum score
Adam Watts 7
Henry Lloyd 10
Lucy Smith 9
这是我正在使用的代码:
print("Maximum score")
for key in keys:
print(key, max(Classes[key]))
答案 0 :(得分:0)
假设你的字典是这样的:
students = {'Adam Watts': [3, 7], 'Henry Lloyd': [10, 1], 'Lucy Smith': [9, 9]}
您无法对字典进行排序(字典中的条目没有特定顺序),但您可以将您关注的数据存储在列表中,并对此列表进行排序。
from operator import itemgetter
items = [(student, max(scores)) for student, scores in students.items()]
items.sort(key=itemgetter(1), reverse=True)
for student, score in items:
print(student, score)