按照字典value排序
1 | student_1={ |
输出:
1 | [('d', 8), ('c', 6), ('b', 4), ('a', 2)] |
按list中字典的某个key的value排序
1 | student_2 = [ |
单级排序,仅按照score排序
1 | student_2_sort_1 = sorted(student_2, key=lambda e: e.__getitem__('score')) |
多级排序,先按照score,再按照no排序
1 | student_2_sort_2 = sorted(student_2, key=lambda e:(e.__getitem__('score'), e.__getitem__('no'))) |
输出:
1 | original: [{'score': 90, 'no': 1}, {'score': 90, 'no': 2}, {'score': 88, 'no': 3}, {'score': 92, 'no': 4}] |
降序排序使用:result_list = sorted(student_2, key=lambda e: e.__getitem__('score'), reverse=True)