Python 按第二个值对元组列表进行排序,reverse=True,然后按键,reverse=False

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14466068/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:34:50  来源:igfitidea点击:

Sort a list of tuples by second value, reverse=True and then by key, reverse=False

pythonsortingdictionaryreverse

提问by Nicolas Hung

I need to sort a dictionary by first, values with reverse=True, and for repeating values, sort by keys, reverse=False

我需要首先对字典进行排序,值为reverse=True,对于重复值,按键排序,reverse=False

So far, I have this

到目前为止,我有这个

dict = [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]
sorted(dict.items(), key=lambda x: (x[1],x[1]), reverse=True)

which returns...

返回...

[('B', 3), ('A', 2), ('J', 1), ('I', 1), ('A', 1)]

but I need it to be:

但我需要它是:

[('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]

as you can see, when values are equal, I can only sort the key in a decreasing fashion as specified... But how can I get them to sort in an increasing fashion?

如您所见,当值相等时,我只能按照指定的递减方式对键进行排序......但是我怎样才能让它们以递增的方式排序?

采纳答案by mgilson

The following works with your input:

以下适用于您的输入:

d = [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]
sorted(d,key=lambda x:(-x[1],x[0]))

Since your "values" are numeric, you can easily reverse the sort order by changing the sign.

由于您的“值”是数字,因此您可以通过更改符号轻松颠倒排序顺序。

In other words, this sort puts things in order by value (-x[1]) (the negative sign puts big numbers first) and then for numbers which are the same, it orders according to key (x[0]).

换句话说,这种排序按值 ( -x[1])排序(负号先放大数),然后对于相同的数字,它根据键 ( x[0])排序。

If your values can't so easily be "negated" to put big items first, an easy work-around is to sort twice:

如果您的值不能那么容易地被“否定”以将大项目放在首位,一个简单的解决方法是排序两次:

from operator import itemgetter
d.sort(key=itemgetter(0))
d.sort(key=itemgetter(1),reverse=True)

which works because python's sorting is stable.

之所以有效,是因为 python 的排序是稳定的。

回答by NPE

In [4]: l = [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]

In [5]: sorted(l, key=lambda (x,y):(-y,x))
Out[5]: [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]

回答by Ashwini Chaudhary

you can use collections.defaultdict:

你可以使用collections.defaultdict

In [48]: from collections import defaultdict

In [49]: dic=[('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]

In [50]: d=defaultdict(list)

In [51]: for x,y in dic:
    d[y].append(x)
    d[y].sort()          #sort the list

now dis something like:

现在d是这样的:

 defaultdict(<type 'list'>, {1: ['A', 'I', 'J'], 2: ['A'], 3: ['B']}

i.e. A new dictwith 1,2,3...as keys and corresponding alphabets stored in lists as values.

即一个新的dict1,2,3...作为键和对应的存储在列表作为值字母表。

Now you can iterate over the sorted(d.items)and get the desired result using itertools.chain()and itertools.product().

现在,您可以sorted(d.items)使用itertools.chain()和迭代并获得所需的结果itertools.product()

In [65]: l=[?product(y,[x]) for x,y in sorted(d.items(),reverse=True)]

In [66]: list(chain(*l))
Out[66]: [('B', 3), ('A', 2), ('A', 1), ('I', 1), ('J', 1)]