Python 按最高值对字典进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20304824/
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
Sort dict by highest value?
提问by Lucas Phillips
I've got a dict with string keys and int values. Is there any way I could take this dict and use it to get a list of the keys from highest to lowest value?
我有一个带有字符串键和 int 值的字典。有什么办法可以使用这个字典并使用它来获取从最高值到最低值的键列表?
Example:
例子:
>>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8}
>>> myList = myDict.sortNumericallyByKeys
>>> myList
['eight', 'seven', 'five', 'four', 'two', 'one']
回答by kaspersky
You can use itemsto get a list of pairs key-value and sortedto sort them using your criteria:
您可以使用items来获取键值对列表并sorted使用您的条件对它们进行排序:
myList = sorted(myDict.items(), key=lambda x: x[1], reverse=True)
If you're using ipython, you can type myDict.tabtaband you're get a list of all functions. You can also type print myDict.items.__doc__and get a quick documentation.
如果你使用 ipython,你可以输入 myDict。tabtab你会得到一个所有功能的列表。您还可以键入print myDict.items.__doc__并获取快速文档。
The key parameter is a function to apply to an element before it gets compared. As itemsreturns a list of pairs and sorting is done on pairs' second element, key is a function which gets the second element from a tuple.
关键参数是一个在比较之前应用于元素的函数。由于items返回对列表并在对的第二个元素上进行排序,key 是一个从元组中获取第二个元素的函数。
Of course, it is possible to get rid of the itemscall, using:
当然,可以items使用以下方法摆脱调用:
myList = sorted(myDict, key=myDict.get, reverse=True) #posted in another answer
回答by keyser
Here's one way of doing it:
这是一种方法:
>>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8}
>>> sorted(myDict.iterkeys(), key=lambda k: myDict[k], reverse=True)
['eight', 'seven', 'five', 'four', 'two', 'one']
(Inspired by thisanswer)
(灵感来自这个答案)
It uses the built-in function sorted(with reverse=Trueto get highest to lowest),
and the keyargument is a function that sets the sort key. In this case it's a lambda that fetches the corresponding dict value, but it can be pretty much anything. For example, you could use operator.itemgetter(1)or myDict.getas shown by other answers, or any other sorting function (other than by value).
它使用内置函数sorted(reverse=True从高到低),key参数是设置排序键的函数。在这种情况下,它是一个获取相应 dict 值的 lambda,但它几乎可以是任何东西。例如,您可以使用operator.itemgetter(1)或myDict.get如其他答案所示,或任何其他排序功能(按值除外)。
回答by iruvar
sorted(myDict, key=myDict.get, reverse=True)
回答by siebz0r
Another variation:
另一种变体:
import operator
d = {'q':2, 'x':1, 'b':10}
s = sorted(d.iteritems(), key=operator.itemgetter(1), reverse=True)
The operatormodule provides itemgetterwhich is designed to be used like this. It's faster than a lambda. ;-)
该operator模块提供了itemgetter旨在像这样使用的内容。它比 lambda 快。;-)
Edit:
编辑:
I kinda misinterpreted the question. My solution returns a list of tuples (key, value) instead of just a list of strings.
我有点误解了这个问题。我的解决方案返回一个元组列表(键,值),而不仅仅是一个字符串列表。
As a bonus to make it up, have a look at collections.OrderedDict. You might also want to consider using the dict 'reversed'. e.g. switch the keys and values.
作为弥补的奖励,看看collections.OrderedDict。您可能还想考虑使用 dict 'reversed'。例如切换键和值。

