我有一本字典如下。
{'MADTHNTEYH': ' 7.6518374230562838',
'MADTHNTEYS': ' 7.9421338120705345',
'MADTHNTEYW': ' 7.7303738107448217',
'MALTENAEYS': ' 7.4882848464336709',
'MALTENTEYH': ' 7.1865554139328083',
'MALTHNAEYH': ' 7.5424656020912142',
'MALTHNTEYS': ' 7.8213289476188521',
'MALTNNTEYW': ' 7.3735279637144355',
'MFDTENAEYH': ' 7.3250263600055829',
'MFDTENAEYW': ' 7.4035627476941208',
'MFDTHNAEYS': ' 7.9597998936916268',
'MNLTHNFEYH': ' 7.3567715253480195',
'MNLTNNFEYS': ' 7.4110269317835664',
'MNLTNNFEYW': ' 7.1992669304578536',
'MNPTENAEYH': ' 7.0852200293633736',
'MNPTENAEYW': ' 7.1637564170519115',
'MNPTENTEYH': ' 7.0737869858767608',
'MNPTENTEYS': ' 7.3640833748910115',
'MNPTENTEYW': ' 7.1523233735652987',
'MNPTHNAEYS': ' 7.7199935630494174',
'MNPTNNAEYH': ' 7.193656191456463',
'MNPTNNFEYH': ' 7.0400205169440992',
'MNPTNNFEYS': ' 7.3303169059583491',
'MNPTNNFEYW': ' 7.1185569046326371',
'MNPTNNTEYH': ' 7.1822231479698502',
'MNPTNNTEYS': ' 7.4725195369841009',
'MNPTNNTEYW': ' 7.2607595356583889'}
我想要的是一个输出应该包含前10个(根据值)键及其相应的值按降序排列。我尝试了很多,但我没有得到。 在此示例中,输出应为
MFDTHNAEYS : 7.9597998936916268
MADTHNTEYS : 7.9421338120705345
MALTHNTEYS : 7.8213289476188521
MADTHNTEYW : 7.7303738107448217
像这样到10日。
答案 0 :(得分:2)
为此,您可以使用collections.Counter后跟most_common
>>> from collections import Counter
>>> some_dict = {'MNLTHNFEYH': ' 7.3567715253480195', 'MNPTENAEYH': ' 7.0852200293633736', 'MADTHNTEYH': ' 7.6518374230562838', 'MFDTENAEYH': ' 7.3250263600055829', 'MALTHNAEYH': ' 7.5424656020912142', 'MFDTHNAEYS': ' 7.9597998936916268', 'MALTENTEYH': ' 7.1865554139328083', 'MFDTENAEYW': ' 7.4035627476941208', 'MALTNNTEYW': ' 7.3735279637144355', 'MADTHNTEYW': ' 7.7303738107448217', 'MNPTENAEYW': ' 7.1637564170519115', 'MADTHNTEYS': ' 7.9421338120705345', 'MNLTNNFEYW': ' 7.1992669304578536', 'MNPTHNAEYS': ' 7.7199935630494174', 'MNLTNNFEYS': ' 7.4110269317835664', 'MNPTNNAEYH': ' 7.193656191456463', 'MNPTNNFEYS': ' 7.3303169059583491', 'MNPTNNTEYW': ' 7.2607595356583889', 'MNPTENTEYW': ' 7.1523233735652987', 'MNPTNNFEYW': ' 7.1185569046326371', 'MNPTNNTEYS': ' 7.4725195369841009', 'MNPTENTEYS': ' 7.3640833748910115', 'MNPTNNFEYH': ' 7.0400205169440992', 'MNPTENTEYH': ' 7.0737869858767608', 'MNPTNNTEYH': ' 7.1822231479698502', 'MALTHNTEYS': ' 7.8213289476188521', 'MALTENAEYS': ' 7.4882848464336709'}
>>> for k, v in Counter(some_dict).most_common(10):
print "{}:{}".format(k,v)
MFDTHNAEYS: 7.9597998936916268
MADTHNTEYS: 7.9421338120705345
MALTHNTEYS: 7.8213289476188521
MADTHNTEYW: 7.7303738107448217
MNPTHNAEYS: 7.7199935630494174
MADTHNTEYH: 7.6518374230562838
MALTENAEYS: 7.4882848464336709
MNPTNNTEYS: 7.4725195369841009
MNLTNNFEYS: 7.4110269317835664
MFDTENAEYW: 7.4035627476941208
答案 1 :(得分:2)
我不相信collections.Counter
是必需的:
for key in sorted(d, key=d.get, reverse=True)[:10]:
print '{}: {}'.format(key, d[key])
答案 2 :(得分:2)
Counter
类是dict
的子类,因此使用它会创建副本。由于其most_common
方法是使用heapq
实现的,因此您可以跳过中间人:
http://docs.python.org/2/library/heapq
>>> import heapq
>>> from decimal import Decimal
>>> # d = {...}
>>>
>>> for k, v in heapq.nlargest(10, d.iteritems(), key=lambda x: Decimal(x[1])):
... print "{}:{}".format(k, v)
...
MFDTHNAEYS: 7.9597998936916268
MADTHNTEYS: 7.9421338120705345
MALTHNTEYS: 7.8213289476188521
MADTHNTEYW: 7.7303738107448217
MNPTHNAEYS: 7.7199935630494174
MADTHNTEYH: 7.6518374230562838
MALTHNAEYH: 7.5424656020912142
MALTENAEYS: 7.4882848464336709
MNPTNNTEYS: 7.4725195369841009
MNLTNNFEYS: 7.4110269317835664