多字段指定不同方向排序

Posted yunweixiaoxuesheng

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多字段指定不同方向排序相关的知识,希望对你有一定的参考价值。

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)

以上是关于多字段指定不同方向排序的主要内容,如果未能解决你的问题,请参考以下文章