简体   繁体   English

如何按第二项降序和第一项升序对列表进行排序?

[英]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 的sortedlist.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 的sortedlist.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 支持按元组排序一次,而不是两次:

  • When sorting according to (-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是一种稳定的排序。
  • If the list is long, sorting once with a tuple key is probably faster than sorting twice with simple keys.如果列表很长,使用元组键排序一次可能比使用简单键排序两次更快。 This is especially true because the second key is a string;尤其如此,因为第二个键是字符串; comparing string is slower than comparing integers.比较字符串比比较整数慢。 If you use tuples, the strings will only be compared for two items who are ex aequo on the first key;如果您使用元组,则字符串将仅针对第一个键上相等的两个项目进行比较; but if you sort twice, about 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.

相关问题 如何按第一个条件的降序和第二个条件的升序对列表进行排序? - How to sort a list in descending order for the first criteria and ascending order for the second criteria? 如何在python中按升序排序前半部分,按降序排序后半部分? - How to sort first half in ascending and second half in descending order in python? 如何先按数字降序排序,然后按字母升序排序 - How to sort a list by number first in descending order and then by alphabet in ascending order 在python中对并行列表进行排序-第一项降序,第二项升序 - Sorting parallel lists in python - first item descending, second item ascending 我如何按 seaborn 的升序/降序对直方图条进行排序? - How i can sort histogram bar in ascending/descending order of seaborn? 如何按升序或降序对 matplotlib 图进行排序? - How can i sort the matplotlib graph in ascending or descending order? 根据两个键进行排序,第一个按降序排列,第二个按升序排列 - sorted on basis of two keys, descending order sort for first and ascending for second 如何对三个 Vals 的压缩列表进行排序,其中我想要前两个反转(降序)而第三个不是(升序)? - How do I Sort a Zipped List of Three Vals where I want first two Reversed (Descending) and Third is not (Ascending)? 当输入中给出一个键来决定排序是升序还是降序时,如何按第一个值对元组列表进行排序? - How to sort a list of tuples by the first value when a key is given in the input to decide whether the sort is ascending or descending? 按升序和降序对列表进行排序 - Sort list in ascending and descending order
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM