Python

[python]统计列表中重复项的出现次数

列表项由数字、字符串组成,统计重复项 >>> 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}]: .