@author jackzhenguo
@desc
@date 2019/12/1
Counter对象间可以做数学运算
from collections import Counter
a = ['apple', 'orange', 'computer', 'orange']
b = ['computer', 'orange']
ca = Counter(a)
cb = Counter(b)
#Counter对象间可以做数学运算
ca + cb # Counter({'orange': 3, 'computer': 2, 'apple': 1})
# 进一步抽象,实现多个列表内元素的个数统计
def sumc(*c):
if (len(c) < 1):
return
mapc = map(Counter, c)
s = Counter([])
for ic in mapc: # ic 是一个Counter对象
s += ic
return s
#Counter({'orange': 3, 'computer': 3, 'apple': 1, 'abc': 1, 'face': 1})
sumc(a, b, ['abc'], ['face', 'computer'])