如何运用PYTHON里的Counter
1、from collections import Counter
abc = [1,2,454,3,6,3,1,3,5,6,8,4,3,8,4,2,1,2,3,]
counts = Counter(abc)
print(counts)
引进模块函数,然后直接统计每个数字出现的次数。

2、uuu = counts[6]
print(uuu)
我们可以找到指定的数字出现的次数。

3、none = counts[322]
print(none)
如果没有的数字会显示0。

4、counts[454] = 10
print(counts[454])
print(counts)
可以直接对数字出现的次数进行更改。

5、counts[454] = 0
print(counts[454])
print(counts)
可以吧次数更改为0次。

6、del counts[454]
print(counts)
当然可以直接移除数字。

7、ooo = list(counts.elements())
print(ooo)
可以整理一下为一个新的列表。

8、i = counts.most_common(2)
print(i)
可以查找出现次数最频繁的。

9、i = counts.most_common(2)
print(i[0][0])
出现次数最频繁的具体哪个数字,不需要其他数据也可以这样显示。

10、add = counts + counts
print(add)
su = counts - counts
print(su)
mul = counts * counts
print(mul)
可以进行加减法,但是不能相乘。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:66
阅读量:156
阅读量:158
阅读量:36
阅读量:169