如何获得python词典中的最高键? [英] How do I get the highest key in a python dictionary?
本文介绍了如何获得python词典中的最高键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
d = {'苹果':9,'橙色':3,'葡萄':22}
d = {'apple':9,'oranges':3,'grapes':22}
如何返回最大的键/值?
How do I return the largest key/value?
如何创建一个列表,该列表按从最大到最小的顺序排序?
How do I make a list that has this sorted by largest to lowest value?
推荐答案
>>> d = {'apple':9,'oranges':3,'grapes':22}
>>> v, k = max((v, k) for k, v in d.items())
>>> k
'grapes'
>>> v
22
编辑:对其进行排序:
>>> items = sorted(((v, k) for k, v in d.items()), reverse=True)
>>> items
[(22, 'grapes'), (9, 'apple'), (3, 'oranges')]
这篇关于如何获得python词典中的最高键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文