2012-04-01 23 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

相關問題