9

我有一个清单:

hello = ['1', '1', '2', '1', '2', '2', '7']

我想显示列表中最常见的元素,所以我使用了:

m = max(set(hello), key=hello.count)

但是,我意识到列表中可能有两个元素出现的频率相同,例如上面列表中的 1 和 2。Max 仅输出最大频率元素的第一个实例。

什么样的命令可以检查列表以查看两个元素是否都具有最大实例数,如果是,则将它们都输出?我在这里不知所措。

4

3 回答 3

13

使用类似于您当前的方法,您将首先找到最大计数,然后查找具有该计数的每个项目:

>>> m = max(map(hello.count, hello))
>>> set(x for x in hello if hello.count(x) == m)
set(['1', '2'])

或者,您可以使用 niceCounter类,它可以用来高效地计算东西:

>>> 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'])
于 2012-04-01T01:09:45.490 回答
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']
于 2012-04-01T02:15:07.930 回答
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()功能。我认为这将返回尽可能多的最频繁元素,它通过比较最大频繁元素来工作,当我看到更频繁的元素时,它将删除结果列表,并附加一次;或者如果它是相同的频率,它只是附加到它。并继续下去,直到整个 Counter 被迭代。

于 2012-04-01T01:43:44.213 回答