10

我有一个OrderedDict按值排序的有序字典 ( )。如何获取顶部(比如 25 个)键值并将它们添加到新字典中?例如:我有这样的事情:

dictionary={'a':10,'b':20,'c':30,'d':5}
ordered=OrderedDict(sorted(dictionary.items(), key=lambda x: x[1],reverse=True))

现在ordered是一个有序字典,我想创建一个字典,比如通过获取前 2 个最常见的项目及其键:

frequent={'c':30,'b':20}
4

4 回答 4

16

的主要目的是保留插入collections.OrderedDict元素的顺序。 您在这里想要的是,它具有内置的 n-most-frequent 功能:
collections.Counter

>>> dictionary={'a':10,'b':20,'c':30,'d':5}
>>> import collections
>>> collections.Counter(dictionary).most_common(2)
[('c', 30), ('b', 20)]
于 2011-11-28T18:04:51.140 回答
6

Just make a new dictionary using the first N items (key pairs) in the (reverse) ordered dictionary you already have. For example, to get the top three items you could do something like this:

from collections import OrderedDict
from operator import itemgetter

# create dictionary you have
dictionary = {'a': 10, 'b': 20, 'c': 30, 'd': 5}
ordered = OrderedDict(sorted(dictionary.items(), key=itemgetter(1), reverse=True))

topthree = dict(ordered.items()[:3])
print(topthree) # -> {'a': 10, 'c': 30, 'b': 20}

For Python 3 one could use dict(list(ordered.items())[:3]) since items() returns an iterator in that version. Alternatively you could use dict(itertools.islice(ordered.items(), 3)) which would work in both Python 2 and 3.

Also note the result is just a regular dictionary—as you specified in your question—not a collections.Counter or other type of mapping. This approach is very general and doesn't require the original dictionary to have integer values—just things can be ordered (i.e. compared via the key function).

于 2011-11-27T17:05:27.713 回答
3

您是否尝试过从排序后的元组列表中索引以获得最常见的第 n 个项目及其键?例如,如果您需要前 2 个最常见的项目,您可以这样做

dictionary={'a':10,'b':20,'c':30,'d':5}
ordered=dict(sorted(dictionary.items(), key=lambda x: x[1],reverse=True)[:2])
于 2011-11-27T16:27:28.643 回答
1

从方法中获取项目的迭代器ordered.iteritems()

现在,要获取前 N 个项目,您可以使用islice方法 from itertools

>>> import itertools
>>> toptwo = itertools.islice(ordered.iteritems(), 2)
>>> list(toptwo)
[('c', 30), ('b', 20)]
>>>
于 2011-11-27T16:33:17.623 回答