如何按值(DESC)然后按键(ASC)对字典进行排序?

时间:2021-08-31 07:40:18

Just after discovering the amazing sorted(), I became stuck again.

在发现惊人的排序()之后,我再次陷入困境。

The problem is I have a dictionary of the form string(key) : integer(value) and I need to sort it in descending order of its integer values, but if two elements where to have same value, then by ascending order of key.

问题是我有一个字符串的形式字符串(键):整数(值),我需要按其整数值的降序排序,但如果两个元素在哪里有相同的值,那么按键的升序。

An example to make it clearer:

一个让它更清晰的例子:

d = {'banana':3, 'orange':5, 'apple':5}
out: [('apple', 5), ('orange', 5), ('banana', 3)]

After doing some research I arrived at something like:

做了一些研究之后我得到了类似的东西:

sorted(d.items(), key=operator.itemgetter(1,0), reverse=True)
out: [('orange', 5), ('apple', 5), ('banana', 3)]

This is because it's reverse-sorting both the value and the key. I need the key to be un-reversed.

这是因为它对值和键进行反向排序。我需要钥匙不要反转。

2 个解决方案

#1


27  

Something like

就像是

In [1]: d = {'banana': 3, 'orange': 5, 'apple': 5}

In [2]: sorted(d.items(), key=lambda x: (-x[1], x[0]))
Out[2]: [('apple', 5), ('orange', 5), ('banana', 3)]

#2


-1  

  • Dictionaries can't be sorted directly, so you need to instead sort the items(), the list of tuples containing key/value pairs.

    字典无法直接排序,因此您需要对items(),包含键/值对的元组列表进行排序。

  • Since you want to sort by the value field, then the key fields, it is necessary to extract those field from the tuple for use as the sort key using operator.itemgetter which gets the specified field.

    由于您希望按值字段排序,然后是键字段,因此必须使用获取指定字段的operator.itemgetter从元组中提取这些字段以用作排序键。

  • Lastly, to sort descending on one field and descending on another, do two passes, first sorting by the secondary key ascending, and then another pass sorting by the primary key descending. This step relies on Python's sort stability.

    最后,要对一个字段进行降序排序并对另一个字段进行降序,请执行两次传递,首先按二级键升序排序,然后按主键降序排序另一次传递。此步骤依赖于Python的排序稳定性。

For example:

例如:

import operator
In [1]: d = {'banana': 3, 'orange': 5, 'apple': 5}

In [2]: fruit = sorted(d.items(), key=operator.itemgetter(0))
In [3]: sorted(fruit, key=operator.itemgetter(1), reverse=True)
Out[3]: [('apple', 5), ('orange', 5), ('banana', 3)]

See the Python Sorting-HOWTO guide for more details.

有关更多详细信息,请参阅Python Sorting-HOWTO指南。

#1


27  

Something like

就像是

In [1]: d = {'banana': 3, 'orange': 5, 'apple': 5}

In [2]: sorted(d.items(), key=lambda x: (-x[1], x[0]))
Out[2]: [('apple', 5), ('orange', 5), ('banana', 3)]

#2


-1  

  • Dictionaries can't be sorted directly, so you need to instead sort the items(), the list of tuples containing key/value pairs.

    字典无法直接排序,因此您需要对items(),包含键/值对的元组列表进行排序。

  • Since you want to sort by the value field, then the key fields, it is necessary to extract those field from the tuple for use as the sort key using operator.itemgetter which gets the specified field.

    由于您希望按值字段排序,然后是键字段,因此必须使用获取指定字段的operator.itemgetter从元组中提取这些字段以用作排序键。

  • Lastly, to sort descending on one field and descending on another, do two passes, first sorting by the secondary key ascending, and then another pass sorting by the primary key descending. This step relies on Python's sort stability.

    最后,要对一个字段进行降序排序并对另一个字段进行降序,请执行两次传递,首先按二级键升序排序,然后按主键降序排序另一次传递。此步骤依赖于Python的排序稳定性。

For example:

例如:

import operator
In [1]: d = {'banana': 3, 'orange': 5, 'apple': 5}

In [2]: fruit = sorted(d.items(), key=operator.itemgetter(0))
In [3]: sorted(fruit, key=operator.itemgetter(1), reverse=True)
Out[3]: [('apple', 5), ('orange', 5), ('banana', 3)]

See the Python Sorting-HOWTO guide for more details.

有关更多详细信息,请参阅Python Sorting-HOWTO指南。