2012-04-01 57 views
8

我有一个列表:Python的最大使用相同数量的实例

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

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

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

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

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

回答

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']) 
+0

什么当有次3重复的数字,如[ '1', '1', '2', '2', '8', '7', '7'] ...你的脚本不会为此工作。谢谢,否则解决方案是好的。 – 2012-04-01 01:27:21

+0

@james:无法重现,它会为这两个代码片断返回'set(['1','2','7'])''。 – 2012-04-01 01:29:15

+0

啊,是的,没问题,它现在对我很好。非常感谢。 – 2012-04-01 01:30:30

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()函数,直到我搜索。我认为这将返回尽可能多的最频繁元素,它通过比较最大频繁元素来工作,当我看到更频繁的元素时,它将删除结果列表并追加一次;或者如果它是相同的频率,它只是附加到它。并继续直到整个计数器迭代完成。

+0

这是一个很好的例子!如果您不只是寻找最简单的方法,它会显示如何自己做到这一点。 – agf 2012-04-01 01:54:43

+2

我也学到了一些东西,我学会了'most_common()'函数的工作原理,并将其加入书签,以备将来再次使用该特定函数。所以我们所有人都是双赢的,欢呼! – George 2012-04-01 01:57:33

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

+1,非常干净的解决方案。最后一行可以略微简化为'd [max(d)]':) – 2012-04-01 02:21:56

+1

谢谢,现在看起来更好了:D – jamylak 2012-04-01 02:37:49

+0

这个方法的问题是O(n ** 2)。 'sequence.count'是O(n),你为序列中的每个项目执行一次。“计数器”方法或手工编码的等价物是O(n) - 每个项目的操作数量与序列中的项目数量无关。 – agf 2012-04-01 08:07:06

相关问题