如何按值对字典进行排序? [英] How do I sort a dictionary by value?

查看:56
本文介绍了如何按值对字典进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从数据库中的两个字段读取的值的字典:一个字符串字段和一个数字字段.字符串字段是唯一的,所以它是字典的键.

我可以按键排序,但如何根据值排序?

注意:我在此处阅读了 Stack Overflow 问题如何按字典值对字典列表进行排序? 并且可能可以将我的代码更改为具有字典列表,但是由于我实际上并不需要字典列表,因此我想知道是否有更简单的解决方案来按升序或降序排序.

解决方案

Python 3.7+ 或 CPython 3.6

字典在 Python 3.7+ 中保留插入顺序.在 CPython 3.6 中相同,但这是一个实现细节.

<预><代码>>>>x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}>>>{k: v for k, v in sorted(x.items(), key=lambda item: item[1])}{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

<预><代码>>>>dict(sorted(x.items(), key=lambda item: item[1])){0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

较旧的 Python

无法对字典进行排序,只能获取已排序字典的表示.字典本质上是无序的,但其他类型,例如列表和元组,则不是.所以你需要一个有序的数据类型来表示排序的值,这将是一个列表——可能是一个元组列表.

例如

导入操作符x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}sorted_x = sorted(x.items(), key=operator.itemgetter(1))

sorted_x 将是按每个元组中的第二个元素排序的元组列表.dict(sorted_x) == x.

对于那些希望对键而不是值进行排序的人:

导入操作符x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}sorted_x = sorted(x.items(), key=operator.itemgetter(0))

在Python3中,由于不允许解包我们可以使用

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}sorted_x = sorted(x.items(), key=lambda kv: kv[1])

如果你想要输出作为字典,你可以使用 collections.OrderedDict:

导入集合sorted_dict = collections.OrderedDict(sorted_x)

I have a dictionary of values read from two fields in a database: a string field and a numeric field. The string field is unique, so that is the key of the dictionary.

I can sort on the keys, but how can I sort based on the values?

Note: I have read Stack Overflow question here How do I sort a list of dictionaries by a value of the dictionary? and probably could change my code to have a list of dictionaries, but since I do not really need a list of dictionaries I wanted to know if there is a simpler solution to sort either in ascending or descending order.

解决方案

Python 3.7+ or CPython 3.6

Dicts preserve insertion order in Python 3.7+. Same in CPython 3.6, but it's an implementation detail.

>>> x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
>>> {k: v for k, v in sorted(x.items(), key=lambda item: item[1])}
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

or

>>> dict(sorted(x.items(), key=lambda item: item[1]))
{0: 0, 2: 1, 1: 2, 4: 3, 3: 4}

Older Python

It is not possible to sort a dictionary, only to get a representation of a dictionary that is sorted. Dictionaries are inherently orderless, but other types, such as lists and tuples, are not. So you need an ordered data type to represent sorted values, which will be a list—probably a list of tuples.

For instance,

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(1))

sorted_x will be a list of tuples sorted by the second element in each tuple. dict(sorted_x) == x.

And for those wishing to sort on keys instead of values:

import operator
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=operator.itemgetter(0))

In Python3 since unpacking is not allowed we can use

x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=lambda kv: kv[1])

If you want the output as a dict, you can use collections.OrderedDict:

import collections

sorted_dict = collections.OrderedDict(sorted_x)

这篇关于如何按值对字典进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆