2014-09-19 37 views
1

我有一個格式爲{(f1,f2):counts}的計數器。當我在此上運行Counter.most_common()時,我得到了正確的結果,但是我想爲f2上的某個過濾器過濾most_common()。例如,f2 ='A'應返回f2 ='A'的most_common元素。這個怎麼做?在Python中爲計數器篩選most_common()

+3

在手機上,所以不能確定,但​​嘗試'排序([項目項目在counter.items()如果項目[0] [1] =='A'],鍵= operator.itemgetter(1),反向=真)[:10]' – roippi 2014-09-19 04:24:55

+0

@roippi它的工作。如果你填寫答案,我會接受它。 – codepk 2014-09-19 15:15:28

回答

0

如果我們看一下爲Counter的源代碼,我們看到它使用heapq保持O(n + k log n)其中k是想鍵的數量和nCounter的大小,而不是O(n log n)

def most_common(self, n=None): 
    '''List the n most common elements and their counts from the most 
    common to the least. If n is None, then list all element counts. 

    >>> Counter('abcdeabcdabcaba').most_common(3) 
    [('a', 5), ('b', 4), ('c', 3)] 

    ''' 
    # Emulate Bag.sortedByCount from Smalltalk 
    if n is None: 
     return sorted(self.items(), key=_itemgetter(1), reverse=True) 
    return _heapq.nlargest(n, self.items(), key=_itemgetter(1)) 

因爲這是超過O(n),我們就可以過濾櫃檯,並得到其項目:

counts = Counter([(1, "A"), (2, "A"), (1, "A"), (2, "B"), (1, "B")]) 

Counter({(f1, f2): n for (f1, f2), n in counts.items() if f2 == "A"}).most_common(2) 
#>>> [((1, 'A'), 2), ((2, 'A'), 1)] 

雖然展開它可能使其稍快,如果該事項:

import heapq 
from operator import itemgetter 

filtered = [((f1, f2), n) for (f1, f2), n in counts.items() if f2 == "A"] 
heapq.nlargest(2, filtered, key=itemgetter(1)) 
#>>> [((1, 'A'), 2), ((2, 'A'), 1)]