我有一个从数据库中读取的键值对字典:一个字符串字段和一个数字字段。字符串字段是唯一的,所以它是字典的键。
我可以按照键排序,但是如何基于值排序呢?
注意:我已经在Stack Overflow上阅读了这里的问题:How do I sort a list of dictionaries by a value of the dictionary?,并且可能可以更改我的代码以使用字典列表,但由于我实际上不需要字典列表,我想知道是否有更简单的解决方案来升序或降序排序。
我有一个从数据库中读取的键值对字典:一个字符串字段和一个数字字段。字符串字段是唯一的,所以它是字典的键。
我可以按照键排序,但是如何基于值排序呢?
注意:我已经在Stack Overflow上阅读了这里的问题:How do I sort a list of dictionaries by a value of the dictionary?,并且可能可以更改我的代码以使用字典列表,但由于我实际上不需要字典列表,我想知道是否有更简单的解决方案来升序或降序排序。
我遇到了相同的问题,我是这样解决的:
WantedOutput = sorted(MyDict, key=lambda x : MyDict[x])
回答“不可能对字典进行排序”的人没有读清楚问题!实际上,“我可以按键排序,但是如何根据值排序?”明显意味着他想要一个按照它们的值排序的键列表。
请注意顺序未被定义(具有相同值的键将以任意顺序出现在输出列表中)。
Counter
。from collections import Counter
x = {'hello': 1, 'python': 5, 'world': 3}
c = Counter(x)
print(c.most_common())
>> [('python', 5), ('world', 3), ('hello', 1)]
Counter({'hello':1, 'python':5, 'world':300}).most_common()
返回 [('world', 300), ('python', 5), ('hello', 1)]
。实际上,这适用于任何可排序的值类型(尽管许多其他 Counter 操作需要将值与 int 进行比较)。 - lvcfrom collections import OrderedDict
# regular unsorted dictionary
d = {'banana': 3, 'apple':4, 'pear': 1, 'orange': 2}
# dictionary sorted by key
OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])
# dictionary sorted by value
OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])
从这里复制粘贴: http://docs.python.org/dev/library/collections.html#ordereddict-examples-and-recipes
享受吧 ;-)
这是代码:
import operator
origin_list = [
{"name": "foo", "rank": 0, "rofl": 20000},
{"name": "Silly", "rank": 15, "rofl": 1000},
{"name": "Baa", "rank": 300, "rofl": 20},
{"name": "Zoo", "rank": 10, "rofl": 200},
{"name": "Penguin", "rank": -1, "rofl": 10000}
]
print ">> Original >>"
for foo in origin_list:
print foo
print "\n>> Rofl sort >>"
for foo in sorted(origin_list, key=operator.itemgetter("rofl")):
print foo
print "\n>> Rank sort >>"
for foo in sorted(origin_list, key=operator.itemgetter("rank")):
print foo
以下是结果:
原始的
{'name': 'foo', 'rank': 0, 'rofl': 20000}
{'name': 'Silly', 'rank': 15, 'rofl': 1000}
{'name': 'Baa', 'rank': 300, 'rofl': 20}
{'name': 'Zoo', 'rank': 10, 'rofl': 200}
{'name': 'Penguin', 'rank': -1, 'rofl': 10000}
Rofl
{'name': 'Baa', 'rank': 300, 'rofl': 20}
{'name': 'Zoo', 'rank': 10, 'rofl': 200}
{'name': 'Silly', 'rank': 15, 'rofl': 1000}
{'name': 'Penguin', 'rank': -1, 'rofl': 10000}
{'name': 'foo', 'rank': 0, 'rofl': 20000}
排名
{'name': 'Penguin', 'rank': -1, 'rofl': 10000}
{'name': 'foo', 'rank': 0, 'rofl': 20000}
{'name': 'Zoo', 'rank': 10, 'rofl': 200}
{'name': 'Silly', 'rank': 15, 'rofl': 1000}
{'name': 'Baa', 'rank': 300, 'rofl': 20}
尝试以下方法。我们定义一个名为 mydict 的字典,其中包含以下数据:
mydict = {'carl':40,
'alan':2,
'bob':1,
'danny':3}
如果想要按键进行字典排序,可以进行以下操作:
for key in sorted(mydict.iterkeys()):
print "%s: %s" % (key, mydict[key])
这应该返回以下输出:
alan: 2
bob: 1
carl: 40
danny: 3
另一方面,如果要按值对字典进行排序(正如问题所要求的那样),可以执行以下操作:
for key, value in sorted(mydict.iteritems(), key=lambda (k,v): (v,k)):
print "%s: %s" % (key, value)
这个命令的结果(按值排序字典)应该返回以下内容:
bob: 1
alan: 2
danny: 3
carl: 40
for key, value in sorted(mydict.iteritems(), key=lambda (k,v): v["score"]):
允许您按子键排序。 - Andomar你可以创建一个“倒排索引”,也称为
from collections import defaultdict
inverse= defaultdict( list )
for k, v in originalDict.items():
inverse[v].append( k )
现在你的倒转具有值;每个值都有一组适用的键。for k in sorted(inverse):
print k, inverse[k]
你可以使用 collections.Counter。注意,这适用于数值和非数值类型的值。
>>> x = {1: 2, 3: 4, 4:3, 2:1, 0:0}
>>> from collections import Counter
>>> #To sort in reverse order
>>> Counter(x).most_common()
[(3, 4), (4, 3), (1, 2), (2, 1), (0, 0)]
>>> #To sort in ascending order
>>> Counter(x).most_common()[::-1]
[(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)]
>>> #To get a dictionary sorted by values
>>> from collections import OrderedDict
>>> OrderedDict(Counter(x).most_common()[::-1])
OrderedDict([(0, 0), (2, 1), (1, 2), (4, 3), (3, 4)])
from collections import Counter
x = {'hello':1, 'python':5, 'world':3}
c=Counter(x)
print( c.most_common() )
>> [('python', 5), ('world', 3), ('hello', 1)]
你也可以使用一个自定义函数,该函数可以传递给参数 key。
def dict_val(x):
return x[1]
x = {1: 2, 3: 4, 4: 3, 2: 1, 0: 0}
sorted_x = sorted(x.items(), key=dict_val)