counter计数器
import collections as con st = '1324243598234598756' tup = (4,67,7,5,6,67,78,8,4,2,2,4, 4,56,7,8,5,3,23,45,468,9,1,) lis = [1,4,5,7,8,2,2,1,56,7,89,34,2,3,4,5,6,5,8,7,4,12,1] # 计算序列某元素出现的次数 ret = con.Counter(st) print(ret,'str') ret = con.Counter(tup) print(ret,'tuple') ret = con.Counter(lis) print(ret,'list') # 取最多出现的元素,参数为取前几个 ret1 = ret.most_common(5) print(ret,ret1,'most_common') # 循环取排列后的所有元素 for i in ret.elements(): print(i,'elements') # 循环取计数后的键值对(i=键,m=键值) for i,m in ret.items(): print(i,m,'items()') # 循环取计数后的键 for i in ret.keys(): print(i,'keys') # 循环取计数后的键值 for i in ret.values(): print(i,'values') # 计数一个新序列,将新计次结果和原有基础相加 ret = con.Counter(st) ret.update(st) print(ret,'update') # 计数一个新序列,在原有基础上减去新计次结果(如果原有基础没有新序列的某个元素,那么就是0-n) ret = con.Counter(st) ret.subtract('7asdd') print(ret,'subtract')