简体   繁体   English

使用排序的内置函数对python dict的值进行排序

[英]sorting values of python dict using sorted builtin function

I need to get a sorted representation of a dict ,sorted in the descending order of values (largest value in dict to be shown first). 我需要得到一个dict的有序表示,按值的降序排序(dict中的最大值,首先显示)。

sample: 样品:

mydict={u'jon':30,u'den':26,u'rob':42,u'jaime':31}

I need to show them like 我需要向他们展示

rob=42
jaime=31
jon=30
den=28

I tried this 我试过这个

from operator import itemgetter
sortedvalues=sorted(mydict,key=itemgetter(1))

When I print the list I get 当我打印列表时,我得到了

[u'jaime', u'den', u'rob', u'jon']

This list is unordered! 这个清单是无序的! Am I missing something about the usage of sorted builtin ? 我错过了有关排序内置的用法吗? or am I using the itemgetter incorrectly? 还是我错误地使用了itemgetter?

This is an interesting problem because you didn't cause an error like you would have if the keys were of another non-indexable type (say integers), and this is due to a subtle series of things: 这是一个有趣的问题,因为如果键是另一个不可索引的类型(比如整数),就不会出现错误,这是由于一系列微妙的事情:

  1. sorted(mydict, ...) tries to iterate a dictionary using the equivalent of iter(mydict) which will call mydict.__iter__() sorted(mydict,...)尝试使用等效的iter(mydict)迭代字典,它将调用mydict.__iter__()
  2. Iterating a dictionary yields its keys , in effect iter(mydict) is the same as mydict.iterkeys() . 迭代字典产生其 ,实际上iter(mydict)mydict.iterkeys()相同。
  3. Your keys were strings, and since strings are indexable, itemgetter(1) will work on a string value, getting the second character in the string. 你的键是字符串,因为字符串是可索引的,所以itemgetter(1)将处理字符串值,获取字符串中的第二个字符。

The code you had would've failed with an IndexError if any strings had a 1-char length as well, you just lucked by. 如果任何字符串都有1个字符长度,那么你所拥有的代码会因为IndexError而失败,你只是幸运的。 (or didn't, depending on how you look at it, since getting the IndexError would've made you aware of the problem sooner.) (或者没有,取决于你如何看待它,因为获得IndexError会让你更快意识到这个问题。)

What you want to do if you only want the values is: 如果您只想要值,您想要做的是:

sorted(mydict.values(), reverse=True)

And if you want the keys as well in pairs, you want 如果你想要成对的钥匙,你想要的

sorted(mydict.iteritems(), key=itemgetter(1), reverse=True)

They're sorted by the second letter in the name; 它们按名称中的第二个字母排序; iterating over a dict yields its keys. 迭代dict产生它的键。

sorteditems = sorted(mydict.iteritems(), key=itemgetter(1))

Iterating over a dictionary (which is what the sorted function does) will give you only it's keys: 迭代字典(这是sorted函数所做的)将只给你它的键:

>>> sorted(mydict)
[u'den', u'jaime', u'jon', u'rob']

Instead you want to sort both the keys and values - to do this, you would use mydict.items() (or mydict.iteritems() , which is more efficient with large dicts): 相反,你想要对键和值进行排序 - 为此,你可以使用mydict.items() (或mydict.iteritems() ,这对于大型mydict.iteritems()更有效):

>>> sorted(mydict.items())
[(u'den', 26), (u'jaime', 31), (u'jon', 30), (u'rob', 42)]

Then your code would work as expected: 然后您的代码将按预期工作:

>>> from operator import itemgetter
>>> sorted(mydict.items(), key = itemgetter(1))
[(u'den', 26), (u'jon', 30), (u'jaime', 31), (u'rob', 42)]

You may also want to sort with the dict's key as the secondary sort value, in case multiple keys have the same value: 如果多个键具有相同的值,您可能还希望使用dict的键作为辅助排序值进行排序:

>>> mydict={u'a': 1, 'z': 1, 'd': 1}
>>> sorted(mydict.items(), key = itemgetter(1))
[(u'a', 1), ('z', 1), ('d', 1)]
>>> sorted(mydict.items(), key = itemgetter(1, 0))
[(u'a', 1), ('d', 1), ('z', 1)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM