列表项由数字、字符串组成,统计重复项:
>>> from collections import defaultdict
>>> d = defaultdict(int)
>>> for x in [1, 2, 3, 1, 2, 3, 1]:
... d[x] += 1
...
>>> dict(d)
{1: 3, 2: 2, 3: 2}
>>>
>>> c = defaultdict(int)
>>> for y in ['a', 'b', 'a', 'c', 'c']:
... c[y] += 1
...
>>> dict(c)
{'a': 2, 'c': 2, 'b': 1}
列表项由字典组成,统计某一键值的重复数:
>>> e = defaultdict(int)
>>> for x in [{'a': 1, 'b': 1}, {'a': 2, 'b':1}, {'a': 1, 'c': 3}]:
... e[x['a']] += 1
...
>>> dict(e) # 'a': 1 出现2次,'a': 2 出现1次
{1: 2, 2: 1}
Comments