手抄报 安全手抄报 手抄报内容 手抄报图片 英语手抄报 清明节手抄报 节约用水手抄报

如何运用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
© 手抄报圈