在Python中对元素计数器进行分组的最有效方法

时间:2014-05-29 15:22:01

标签: python algorithm counter frequency

假设我有一个像这样的列表/元组:['a', 'b', 'c', 'a', 'b', 'b', 'c']

Frequency of 'a': 2
Frequency of 'b': 3
Frequency of 'c': 2

现在我想按照这些频率对它们进行分组,预期输出:{2: ['a', 'c'], 3: ['b']}或其他任何我可以轻松获取频率及其项目的内容。

我怎样才能以最有效的方式做到这一点?

3 个答案:

答案 0 :(得分:7)

您可以将collections.Countercollections.defaultdict

一起使用
>>> from collections import Counter, defaultdict
>>> l = ['a', 'b', 'c', 'a', 'b', 'b', 'c']
>>> 
>>> counts = Counter(l)
>>> freqs = defaultdict(list)
>>> 
>>> for k,v in counts.items():
...     freqs[v].append(k)
... 
>>> freqs
defaultdict(<type 'list'>, {2: ['a', 'c'], 3: ['b']})

答案 1 :(得分:0)

使用collections.Counteritertools.groupby

>>> from collections import Counter
>>> from itertools import groupby
>>>
>>> freq = Counter(['a', 'b', 'c', 'a', 'b', 'b', 'c']).most_common()
>>> {key:[x for x, _ in grp]
...  for key, grp in groupby(freq, key=lambda tup: tup[1])}
{2: ['a', 'c'], 3: ['b']}

答案 2 :(得分:0)

我会稍微改变一下。

from collections import defaultdict

items = ['a', 'b', 'c', 'a', 'b', 'b', 'c']
counts = defaultdict(list)
for item in set(items):
    counts[items.count(item)].append(item)
dict(counts)

计数然后看起来像这样

{2: set(['a', 'c']), 3: set(['b'])}