[英]Get most frequent item in python dictionary given frequencies
How do I return the most commonly occurring element in a dictionary given the frequencies of each element? 在给定每个元素的频率的情况下,如何返回字典中最常出现的元素? For example, in the following list, I want to return the most frequently occurring element by the first frequency and the most frequently occurring element by the second frequency? 例如,在下面的列表中,我想通过第一个频率返回最频繁出现的元素,并且通过第二个频率返回最频繁出现的元素?
dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50] }
So the method findMostFreqFirst(dictionary)
would return "first" and the method findMostFreqSecond
would return "third." 因此方法findMostFreqFirst(dictionary)
将返回“first”,方法findMostFreqSecond
将返回“third”。 Is there a way I can do this using the most efficient amount of code possible? 有没有办法可以使用最有效的代码量来做到这一点? (I'm writing this as part of a much larger program so I don't want to write a copious amount of code for these two functions. Thanks! (我写这篇文章是一个更大的程序的一部分,所以我不想为这两个函数编写大量的代码。谢谢!
Use max
with key
keyword argument: 使用max
with key
keyword参数:
>>> dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50] }
>>> max(dictionary, key=lambda key: dictionary[key][0])
'first'
>>> max(dictionary, key=lambda key: dictionary[key][1])
'third'
The first one can be written as follow because list comparison are done lexicographically. 第一个可以写成如下,因为列表比较是按字典顺序完成的。 ( [30, 40] > [20, 50]
) ( [30, 40] > [20, 50]
)
>>> max(dictionary, key=dictionary.get)
'first'
You can all at once this way. 你可以这样做。
First element: 第一要素:
>>> dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50] }
>>> sorted(dictionary, key=lambda key: dictionary[key][0], reverse=True)
['first', 'third', 'second']
Then use an index to the sorted list to return the element in question: 然后使用索引到排序列表以返回有问题的元素:
>>> sorted(dictionary, key=lambda key: dictionary[key][0], reverse=True)[0]
'first'
Second element: 第二个要素:
>>> sorted(dictionary, key=lambda key: dictionary[key][1], reverse=True)
['third', 'first', 'second']
If you want the second element to break a tie with the first: 如果你想让第二个元素与第一个元素打成平局:
>>> dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50],
... "fourth":[30,60]}
>>> sorted(dictionary, key=lambda key: dictionary[key][0:2], reverse=True)
['fourth', 'first', 'third', 'second']
A bit late to the table, but an approach that can handle an arbitrary number of "columns" with varying lengths would be: 该表稍晚,但是可以处理具有不同长度的任意数量的“列”的方法将是:
dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50] }
from itertools import izip_longest
keys, vals = zip(*dictionary.items())
items = izip_longest(*vals, fillvalue=0)
print [keys[max(xrange(len(item)), key=item.__getitem__)] for item in items]
# ['first', 'third']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.