我有一个清单:
hello = ['1', '1', '2', '1', '2', '2', '7']
我想显示列表中最常见的元素,所以我使用了:
m = max(set(hello), key=hello.count)
然而,我意识到列表中可能有两个出现相同频率的元素,如上面列表中的1和2。 Max仅输出最大频率元素的第一个实例。
哪种命令可以检查列表以查看两个元素是否都具有最大实例数,如果是,则将它们输出?我在这里不知所措。
答案 0 :(得分:13)
使用类似于当前的方法,您首先会找到最大数量,然后查找具有该数量的每个项目:
>>> m = max(map(hello.count, hello))
>>> set(x for x in hello if hello.count(x) == m)
set(['1', '2'])
或者,你可以使用漂亮的Counter
类,它可以用来有效地计算内容:
>>> hello = ['1', '1', '2', '1', '2', '2', '7']
>>> from collections import Counter
>>> c = Counter(hello)
>>> c
Counter({'1': 3, '2': 3, '7': 1})
>>> common = c.most_common()
>>> common
[('1', 3), ('2', 3), ('7', 1)]
然后,您可以使用列表推导来获取具有最大计数的所有项目:
>>> set(x for x, count in common if count == common[0][1])
set(['1', '2'])
答案 1 :(得分:3)
编辑:更改了解决方案
>>> from collections import Counter
>>> from itertools import groupby
>>> hello = ['1', '1', '2', '1', '2', '2', '7']
>>> max_count, max_nums = next(groupby(Counter(hello).most_common(),
lambda x: x[1]))
>>> print [num for num, count in max_nums]
['1', '2']
答案 2 :(得分:2)
from collections import Counter
def myFunction(myDict):
myMax = 0 # Keep track of the max frequence
myResult = [] # A list for return
for key in myDict:
print('The key is', key, ', The count is', myDict[key])
print('My max is:', myMax)
# Finding out the max frequence
if myDict[key] >= myMax:
if myDict[key] == myMax:
myMax = myDict[key]
myResult.append(key)
# Case when it is greater than, we will delete and append
else:
myMax = myDict[key]
del myResult[:]
myResult.append(key)
return myResult
foo = ['1', '1', '5', '2', '1', '6', '7', '10', '2', '2']
myCount = Counter(foo)
print(myCount)
print(myFunction(myCount))
输出:
The list: ['1', '1', '5', '2', '1', '6', '7', '10', '2', '2']
Counter({'1': 3, '2': 3, '10': 1, '5': 1, '7': 1, '6': 1})
The key is 10 , The count is 1
My max is: 0
The key is 1 , The count is 3
My max is: 1
The key is 2 , The count is 3
My max is: 3
The key is 5 , The count is 1
My max is: 3
The key is 7 , The count is 1
My max is: 3
The key is 6 , The count is 1
My max is: 3
['1', '2']
我写了这个简单的程序,我想它也可能有效。在我进行搜索之前,我不知道most_common()
函数。我认为这将返回尽可能多的频繁元素,它通过比较最大频繁元素来工作,当我看到更频繁的元素时,它将删除结果列表,并将其追加一次;或者如果它是相同的频率,它只是附加到它。并继续前进,直到整个计数器迭代完毕。