0

我有一本像下面这样的字典。

{'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号。

4

3 回答 3

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
于 2013-03-09T12:10:48.013 回答
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
于 2013-03-09T13:06:32.143 回答
2

我不相信collections.Counter是必需的:

for key in sorted(d, key=d.get, reverse=True)[:10]:
    print '{}: {}'.format(key, d[key])
于 2013-03-09T12:18:52.957 回答