假设我有一个像这样的列表/元组:['a', 'b', 'c', 'a', 'b', 'b', 'c']
。
Frequency of 'a': 2
Frequency of 'b': 3
Frequency of 'c': 2
现在我想按照这些频率对它们进行分组,预期输出:{2: ['a', 'c'], 3: ['b']}
或其他任何我可以轻松获取频率及其项目的内容。
我怎样才能以最有效的方式做到这一点?
答案 0 :(得分:7)
您可以将collections.Counter
与collections.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.Counter
,itertools.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'])}