我是靠谱客的博主 热情蛋挞,最近开发中收集的这篇文章主要介绍collections中的Counter、most_common、items等的用法Counter 和 most_common的用法items 的用发,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
Counter 和 most_common的用法
items 的用发
数据准备
list_test = [
[
[1,2,3],
[4,5,6]
],
[
[1,3,4],
[7,8,9],
[1,3,3]
]
]
result = [x for i in list_test for j in i for x in j]
result
输出结果:
[1, 2, 3, 4, 5, 6, 1, 3, 4, 7, 8, 9, 1, 3, 3]
使用:
from collections import Counter
from collections import Iterable
counter_result = Counter(result)
counter_result
# 从输出结果中可以看出来1出现了3次,2出现了1次,3出现了4次。。。。。。
输出结果:
Counter({1: 3, 2: 1, 3: 4, 4: 2, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1})
items使用:
counter_result.items() # 将字典编程元祖结构
输出结果:
dict_items([(1, 3), (2, 1), (3, 4), (4, 2), (5, 1), (6, 1), (7, 1), (8, 1), (9, 1)])
判断类型:
print('是否是可迭代对象n', isinstance(counter_result.items(), Iterable))
# print('typen', type(counter_result.items()))
for i in counter_result.items():
print(i)
输出结果:
是否是可迭代对象
True
(1, 3)
(2, 1)
(3, 4)
(4, 2)
(5, 1)
(6, 1)
(7, 1)
(8, 1)
(9, 1)
最后
以上就是热情蛋挞为你收集整理的collections中的Counter、most_common、items等的用法Counter 和 most_common的用法items 的用发的全部内容,希望文章能够帮你解决collections中的Counter、most_common、items等的用法Counter 和 most_common的用法items 的用发所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复