[英]How can i sort a list by the second item descending and first one ascending?
I have a list like this:我有一个这样的列表:
list_results=[('Horror', 2), ('Romance', 2), ('Comedy', 2), ('History', 2), ('Adventure', 1), ('Action', 3)]
I wish to sort the number in descending order and if numbers were the same, according to the name in ascending order.我希望按降序对数字进行排序,如果数字相同,则根据名称升序排列。
I tried the following code:我尝试了以下代码:
sortlist=sorted(list_results,key=lambda x:(x[1],x[0]))
and the reverse but I couldn't figure out to do it.反之亦然,但我不知道该怎么做。
The answer that I'm looking for is:我正在寻找的答案是:
[('Action', 3), ('Comedy', 2) ,('History', 2),('Horror', 2), ('Romance', 2), ('Adventure', 1), ]
You want to sort according to two criteria, with one criterion acting as a tie-breaker for the other.您想根据两个标准进行排序,其中一个标准充当另一个标准的决胜局。 Since python's
sorted
and list.sort
are guaranteed to be stable sorts, one solution is to sort the list twice: first sort it by the tie-breaker, then sort it by the main criterion.由于 python 的
sorted
和list.sort
保证是稳定的排序,因此一种解决方案是对列表进行两次排序:首先按 tie-breaker 对其进行排序,然后按主要标准对其进行排序。 This is @Bharel's answer.这是@Bharel 的答案。
Another possibility is to sort only once, using a tuple as the key.另一种可能性是只排序一次,使用元组作为键。 Python's
sorted
and list.sort
both offer a reverse= True or False
argument to specify a sort in increasing or decreasing order; Python 的
sorted
和list.sort
都提供reverse= True or False
参数来指定按升序或降序排序; but in your case, we want to sort in decreasing order with respect to the first criterion, and increasing order with respect to the second criterion.但是在您的情况下,我们希望根据第一个标准按降序排序,并根据第二个标准按升序排序。 The
reverse
keyword is not helpful because it is all-or-nothing: it will not allow us to choose which criterion to reverse. reverse
关键字没有帮助,因为它是全有或全无:它不允许我们选择要反转的标准。
Since the first criterion is numeric (an integer), a simple trick to sort in reverse order is to negate it with a minus sign:由于第一个标准是数字(整数),一个简单的逆序排序技巧是用减号取反:
sortlist = sorted(list_results, key=lambda x:(-x[1], x[0]))
Note the -x[1]
instead of just x[1]
.注意
-x[1]
而不是x[1]
。
Here are two arguments in favour of sorting once by a tuple, rather than twice:这里有两个 arguments 支持按元组排序一次,而不是两次:
(-x[1], x[0])
, it is immediately clear that -x[1]
is the main criterion, and x[0]
is only a tie-breaker.(-x[1], x[0])
排序时,立即清楚-x[1]
是主要标准,而x[0]
只是一个平局。 By contrast, if you sort twice, someone reading your code needs to take a second to understand that the last sort is the most important, and the previous sort is only there as a tie-breaker relying on sorted
being a stable sort.sorted
是一种稳定的排序。n log(n)
string comparisons will be performed in the first sort.n log(n)
字符串比较将在第一次排序中执行。 If your list is small, it probably doesn't matter which version is faster (unless you're repeatedly sorting lots of small lists...), so it's a matter of preference and readability.如果您的列表很小,那么哪个版本更快可能并不重要(除非您反复对许多小列表进行排序......),所以这是一个偏好和可读性的问题。
First sort the list by the first item, then by the second item:首先按第一项排序列表,然后按第二项排序:
list_results = sorted(list_results, key=lambda x:x[0])
list_results = sorted(list_results, key=lambda x:x[1], reverse=True)
or better yet without copying:或者更好的是不复制:
import operator
list_results.sort(key=operator.itemgetter(0))
list_results.sort(key=operator.itemgetter(1), reverse=True)
Python's sort algorithm is Timsort . Python 的排序算法是Timsort 。 It is a stable algorithm meaning if 2 values are the same, they'll stay in their original order.
这是一个稳定的算法,这意味着如果 2 个值相同,它们将保持原来的顺序。
If you sort alphabetically first, and then by the priority, the list will be sorted according to the alphabet, then re-sorted according to priority with alphabet being secondary.如果先按字母排序,再按优先级排序,则列表会先按字母排序,再按优先级重新排序,字母次之。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.