按最高值排序 [英] Sort dict by highest value?
本文介绍了按最高值排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
示例:
>>> myDict = {'seven':7,'four':4,'one':1,'two':2,'five':5,'eight':8}
>>> myList = myDict.sortNumericallyByKeys
>>> myList
['eight','seven','five','four','two','one']
解决方案
sorted(myDict,key = myDict.get,reverse = True)
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?
Example:
>>> myDict = {'seven': 7, 'four': 4, 'one': 1, 'two': 2, 'five': 5, 'eight': 8}
>>> myList = myDict.sortNumericallyByKeys
>>> myList
['eight', 'seven', 'five', 'four', 'two', 'one']
解决方案
sorted(myDict, key=myDict.get, reverse=True)
这篇关于按最高值排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文