[英]Get first N key pairs from an Ordered Dictionary to another one
I have an ordered dictionary ( OrderedDict
) sorted by value. 我有一个按值排序的有序字典(
OrderedDict
)。 How can I get the top (say 25) key values and add them to a new dictionary? 如何获得顶部(比如说25个)键值并将它们添加到新词典中? For example: I have something like this:
例如:我有这样的事情:
dictionary={'a':10,'b':20,'c':30,'d':5}
ordered=OrderedDict(sorted(dictionary.items(), key=lambda x: x[1],reverse=True))
Now ordered
is an ordered dictionary, I want to create a dictionary, say by taking the top 2 most-frequent items and their keys: 现在
ordered
是一个有序的字典,我想创建一个字典,比如通过获取前2个最频繁的项目及其键:
frequent={'c':30,'b':20}
The primary purpose of collections.OrderedDict
is retaining the order in which the elements were inserted . collections.OrderedDict
的主要目的是保留插入元素的顺序。
What you want here is collections.Counter
, which has the n-most-frequent functionality built-in: 你想要的是
collections.Counter
,它内置了n个最常用的功能:
>>> dictionary={'a':10,'b':20,'c':30,'d':5}
>>> import collections
>>> collections.Counter(dictionary).most_common(2)
[('c', 30), ('b', 20)]
Just make a new dictionary using the first N items (key pairs) in the (reverse) ordered dictionary you already have. 只需使用您已经拥有的(反向)有序词典中的前N个项目(密钥对)创建一个新词典。 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. 对于Python 3,可以使用
dict(list(ordered.items())[:3])
因为items()
返回该版本中的迭代器。 Alternatively you could use dict(itertools.islice(ordered.items(), 3))
which would work in both Python 2 and 3. 或者你可以使用
dict(itertools.islice(ordered.items(), 3))
,它可以在Python 2和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. 另请注意,结果只是一个常规字典 - 正如您在问题中指定的那样 - 不是
collections.Counter
或其他类型的映射。 This approach is very general and doesn't require the original dictionary
to have integer values—just things can be ordered (ie compared via the key
function). 这种方法非常通用,并且不要求原始
dictionary
具有整数值 - 只需要对事物进行排序(即通过key
函数进行比较)。
Have you tried indexing the List of tuples from the sorted to get the top nth most frequent items and their keys? 您是否尝试从排序中索引元组列表以获取最常见的第n个项目及其密钥? For example, if you need the top 2 most frequent items, you might do
例如,如果您需要前2个最常用的项目,您可能会这样做
dictionary={'a':10,'b':20,'c':30,'d':5}
ordered=dict(sorted(dictionary.items(), key=lambda x: x[1],reverse=True)[:2])
Get the iterator of the items from ordered.iteritems()
method. 从
ordered.iteritems()
方法获取项目的迭代器。
Now, to take the first N items, you may use islice
method from itertools
. 现在,要获取前N个项目,您可以使用
itertools
islice
方法。
>>> import itertools
>>> toptwo = itertools.islice(ordered.iteritems(), 2)
>>> list(toptwo)
[('c', 30), ('b', 20)]
>>>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.