students = [{'name': 'Jack', 'age': 17, 'score': 89}, {'name': 'Julia', 'age': 17, 'score': 80}, {'name': 'Tom', 'age': 16, 'score': 80}] # 按分数排序 students.sort(key=lambda student: student['score']) print(students)
# 按分数和年龄排序 students.sort(key=lambda student: (student['score'], student['age'])) print(students) # 按分数降序和年龄升序 students.sort(key=lambda student: (-student['score'], student['age'])) print(students) #[{'name': 'Julia', 'age': 17, 'score': 80}, {'name': 'Tom', 'age': 16, 'score': 80}, {'name': 'Jack', 'age': 17, 'score': 89}] #[{'name': 'Tom', 'age': 16, 'score': 80}, {'name': 'Julia', 'age': 17, 'score': 80}, {'name': 'Jack', 'age': 17, 'score': 89}] #[{'name': 'Jack', 'age': 17, 'score': 89}, {'name': 'Tom', 'age': 16, 'score': 80}, {'name': 'Julia', 'age': 17, 'score': 80}]
标签:name,17,age,score,student,80,排序,字典 From: https://www.cnblogs.com/daizichuan/p/17529427.html