多字段指定不同方向排序

students = [
    {'name':'a','score':'100','height':100,'weight':180},
    {'name':'b','score':'90','height':90,'weight':130},
    {'name':'c','score':'100','height':100,'weight':120},
    {'name':'d','score':'90','height':90,'weight':120},
    {'name':'e','score':'100','height':100,'weight':150},
    {'name':'f','score':'90','height':90,'weight':150},
]
l = sorted(students,key=lambda x:[x['score'] * -1,x['height'] * -1,x['weight']])

students.sort(key=lambda x: [x.get('score') * -1, x.get('height') * -1, x.get('weight')])
for i in students:
    print(i)
    
print('-'*99)
for i in l:
    print(i)

posted on 2019-11-13 15:21  运维小学生  阅读(131)  评论(0编辑  收藏  举报

导航