如何计算python字典中的最高键?(非常pythonic方式?)编辑:列表

TIM*_*MEX 2 python dictionary

d = {'apple':9,'oranges':3,'grape':22}

如何返回最大的键/值?

编辑:如何创建一个按从最大到最小值排序的列表?

Fog*_*ird 10

>>> d = {'apple':9,'oranges':3,'grapes':22}
>>> v, k = max((v, k) for k, v in d.items())
>>> k
'grapes'
>>> v
22
Run Code Online (Sandbox Code Playgroud)

编辑:要对它们进行排序:

>>> items = sorted(((v, k) for k, v in d.items()), reverse=True)
>>> items
[(22, 'grapes'), (9, 'apple'), (3, 'oranges')]
Run Code Online (Sandbox Code Playgroud)