简体   繁体   English

Python3 asc desc排序

[英]Python3 asc desc sorting

Lets assume I've got a following python list(this is only example): 假设我有以下python列表(这仅是示例):

my_list = [{'user': 'Joe', 'score': 14},
           {'user': 'Foo', 'score': 12},
           {'user': 'May', 'score': 12},
           {'user': 'Kat', 'score': 12},
           {'user': 'Doe', 'score': 13}]

I need to sort this list in ascending order by score and descending order by a username. 我需要按分数按升序排序,按用户名按降序排序。 Expected sort result: 预期的排序结果:

my_list = [{'user': 'May', 'score': 12},
           {'user': 'Kat', 'score': 12},
           {'user': 'Foo', 'score': 12},
           {'user': 'Doe', 'score': 13},
           {'user': 'Joe', 'score': 14}]

So, I could do something like this if I want everything to be in ascending order: 因此,如果我希望所有内容都按升序排列,则可以执行以下操作:

my_list.sort(key=lambda x: (x['score'], x['user']))

For integers it is easy to solve this problem just adding - in front of it: 对于整数,只需添加-就可以轻松解决此问题:

my_list.sort(key=lambda x: (-x['score'], x['user']))

Unfortunately, strings can not be negative :-| 不幸的是,字符串不能为负:-|

I need a generic solution that doesn't involve 'reverse=True'. 我需要一个不涉及“ reverse = True”的通用解决方案。 Lambda function is dynamically generated based on a user config. Lambda函数是根据用户配置动态生成的。

Thoughts? 思考?

Your current solution will work fine if you set the reverse parameter of list.sort to True : 如果将list.sortreverse参数设置为True则当前解决方案可以正常工作:

>>> my_list = [{'user': 'Joe', 'score': 14},
...            {'user': 'Foo', 'score': 12},
...            {'user': 'May', 'score': 12},
...            {'user': 'Kat', 'score': 12},
...            {'user': 'Doe', 'score': 13}]
>>> my_list.sort(key=lambda x: (-x['score'], x['user']), reverse=True)
>>> pprint(my_list) # pprint makes the nice output
[{'score': 12, 'user': 'May'},
 {'score': 12, 'user': 'Kat'},
 {'score': 12, 'user': 'Foo'},
 {'score': 13, 'user': 'Doe'},
 {'score': 14, 'user': 'Joe'}]
>>>

This will sort the list in reverse order. 这将以相反的顺序对列表进行排序。


Edit: 编辑:

Since the names and scores have two different sort orders, you will need to use two separate sorts to achieve your desired output: 由于名称和分数具有两种不同的排序顺序,因此您将需要使用两种单独的排序来实现所需的输出:

>>> my_list = [{'user': 'Joe', 'score': 14},
...            {'user': 'Foo', 'score': 12},
...            {'user': 'May', 'score': 12},
...            {'user': 'Kat', 'score': 12},
...            {'user': 'Doe', 'score': 13}]
>>> my_list.sort(key=lambda x: x['user'], reverse=True)
>>> my_list.sort(key=lambda x: x['score'])
>>> pprint(my_list)
[{'score': 12, 'user': 'May'},
 {'score': 12, 'user': 'Kat'},
 {'score': 12, 'user': 'Foo'},
 {'score': 13, 'user': 'Doe'},
 {'score': 14, 'user': 'Joe'}]
>>>

The ord of a string can be negative: 字符串的ord可以为负:

my_list.sort(key=lambda x: (x["score"],[-ord(x) for x in x["user"]])))


In [50]:  my_list.sort(key=lambda x: (x["score"],[-ord(x) for x in x["user"]]))

In [51]: my_list
Out[51]: 
[{'score': 12, 'user': 'May'},
 {'score': 12, 'user': 'Kat'},
 {'score': 12, 'user': 'Foo'},
 {'score': 13, 'user': 'Doe'},
 {'score': 14, 'user': 'Joe'}]

you need to add the reverse=True keyword for descending order. 您需要添加reverse=True关键字以降序排列。

>>> my_list.sort(key=lambda x: (-x['score'], x['user']),reverse=True)
>>> my_list
[{'score': 12, 'user': 'May'}, {'score': 12, 'user': 'Kat'}, {'score': 12, 'user': 'Foo'}, {'score': 13, 'user': 'Doe'}, {'score': 14, 'user': 'Joe'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM