1
有沒有辦法在計數器中收集發生數量的值?
Python中的計數器
例:
比方說,我有一個列表:
list = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd']
當我這樣做的計數器:
Counterlist = Counter(list)
我會得到:
Counter({'b': 3, 'a': 3, 'c': 3, 'd': 2})
然後我可以選擇讓我們說a並得到3:
Counterlist['a'] = 3
但我怎麼能選擇出現的數字'3'?
喜歡的東西:
Counterlist[3] = ['b', 'a', 'c']
這可能嗎?
'計數器'用於過濾要計數的項目,而不是反過來。最簡單的方法是將它傳遞給'defaultdict(list)'。 – jonrsharpe
'[v for v在Counterlist if Counterlist [v] == 3]' –