如何运用PYTHON里的Counter

 时间:2024-10-13 18:04:06

1、from co盟敢势袂llections import Counterabc = [1,2,454,3,6,3,1,3,5,6,8荑樊综鲶,4,3,8,4,2,1,2,3,]counts = Counter(abc)print(counts)引进模块函数,然后直接统计每个数字出现的次数。

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

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

如何运用PYTHON里的Counter

10、ad蟠校盯昂d = counts + countsprint(add)su = counts - countspr足毂忍珩int(su)mul = counts * countsprint(mul)可以进行加减法,但是不能相乘。

如何运用PYTHON里的Counter
  • PDF一张纸怎么打印多页文件
  • 电脑如何关闭系统默认的共享
  • Windows 系统怎样更改计算机名称
  • 如何在电脑上下载暴雪战网
  • office如何找到并卸载Skype for Bussiness
  • 热门搜索
    关于道德的手抄报 争做文明学生手抄报 卫生手抄报内容 关于团结的手抄报 手抄报小图案 绿色校园手抄报资料 手抄报简单漂亮花边 小学一年级手抄报大全 长城手抄报图片 法制教育手抄报资料