繁体   English   中英

在给定频率的python词典中获取最频繁的项目

[英]Get most frequent item in python dictionary given frequencies

在给定每个元素的频率的情况下,如何返回字典中最常出现的元素? 例如,在下面的列表中,我想通过第一个频率返回最频繁出现的元素,并且通过第二个频率返回最频繁出现的元素?

dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50] }

因此方法findMostFreqFirst(dictionary)将返回“first”,方法findMostFreqSecond将返回“third”。 有没有办法可以使用最有效的代码量来做到这一点? (我写这篇文章是一个更大的程序的一部分,所以我不想为这两个函数编写大量的代码。谢谢!

使用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'

第一个可以写成如下,因为列表比较是按字典顺序完成的。 [30, 40] > [20, 50]

>>> max(dictionary, key=dictionary.get)
'first'

你可以这样做。

第一要素:

>>> dictionary = {"first": [30, 40], "second": [10, 30], "third": [20, 50] }
>>> sorted(dictionary, key=lambda key: dictionary[key][0], reverse=True)
['first', 'third', 'second']

然后使用索引到排序列表以返回有问题的元素:

>>> sorted(dictionary, key=lambda key: dictionary[key][0], reverse=True)[0]
'first'

第二个要素:

>>> sorted(dictionary, key=lambda key: dictionary[key][1], reverse=True)
['third', 'first', 'second']

如果你想让第二个元素与第一个元素打成平局:

>>> 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']

该表稍晚,但是可以处理具有不同长度的任意数量的“列”的方法将是:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM