WordPress网站建设宣传图片

Python Counter()函数介绍 – 统计值出现的次数

释放双眼,带上耳机,听听看~!

Python Counter()函数介绍

Counter()函数 是collections模块(Python标准库中的一个模块)中的里面的一个类,作用是计算出字符串或者列表等中不同元素出现的个数,返回值可以理解为一个字典,所以对传回来的统计结果的操作都可以当作对字典的操作(Counter类继承dict类,所以它能使用dict类里面的方法)

Note: 字符串还有一个内置的count()只能统计字符串中某个元素出现的个数。

PS:
利用这个函数,我们可以轻松一些词频或者是元素统计的需求…
collections模块是Python标准库中的一个模块。

返回值

字典类型,调用Counter()会返回一个key为列表的值value为该值的具体个数的对象。

实例

from collections import Counter
test_str = 'abbcccdddd'
result = Counter(test_str)
print(result)

输出结果

Counter({'d': 4, 'c': 3, 'b': 2, 'a': 1})

结果默认是次数从高到低排序,可以配合使用sorted() 函数(Python3 sorted() 函数 – 对所有可迭代的对象进行排序操作。)进行排序:

from collections import Counter
test_str = 'abbcccdddd'
result = Counter(test_str)
sorted_list = sorted(result.items(), key=lambda x:x[1])
print('sorted_list: ', sorted_list)

输出结果:

sorted_list: [('a', 1), ('b', 2), ('c', 3), ('d', 4)]
sorted()实例代码ipython运行截图

Counter()其他方法

Counter对象支持以下三个字典不支持的方法,update()字典支持

most_common()

返回一个列表,包含counter中n个最大数目的元素,如果忽略n或者为None,most_common()将会返回counter中的所有元素,元素有着相同数目的将会选择出现早的元素

list1 = ["a", "a", "a", "b", "c", "f", "g", "g", "c", "11", "g", "f", "10", "2"]
print(Counter(list1).most_common(3))

输出结果

[('a', 3), ('g', 3), ('c', 2)]

“c”、”f”调换位置,结果变化

list2 = ["a", "a", "a", "b", "f", "c", "g", "g", "c", "11", "g", "f", "10", "2"]
print(Counter(list2).most_common(3))

输出结果

[('a', 3), ('g', 3), ('f', 2)]

elements()

返回一个迭代器,每个元素重复的次数为它的数目,顺序是任意的顺序,如果一个元素的数目少于1,那么elements()就会忽略它

list2 = ["a", "a", "abdz", "abc", "f", "c", "g", "g", "c", "c1a1", "g", "f", "111000", "b10"]
print(''.join(Counter(list2).elements()))
print(''.join(list2))

输出结果

aaabdzabcffccgggc1a1111000b10
aaabdzabcfcggcc1a1gf111000b10
dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}
print(Counter(dic1))
print(list(Counter(dic1).elements()))

输出结果

Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})
['a', 'a', 'a', 'b', 'b', 'b', 'b']

update()

从一个可迭代对象(可迭代对象是一个元素序列,而非(key,value)对构成的序列)中或者另一个映射(或counter)中所有元素相加,是数目相加而非替换它们

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}
dic2 = {'a': 3, 'b': 4, 'c': 0, 'd': 2, "e": -1, "f": 6}
a = Counter(dic1)
print(a)
#结果:Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})
b = Counter(dic2)
print(b)
#结果:Counter({'f': 6, 'b': 4, 'a': 3, 'd': 2, 'c': 0, 'e': -1})
a.update(b)
print(a)
#结果:Counter({'b': 8, 'a': 6, 'f': 6, 'c': 0, 'd': 0, 'e': -1})

subtract()

从一个可迭代对象中或者另一个映射(或counter)中,元素相减,是数目相减而不是替换它们

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2, "e": 0}
dic2 = {'a': 3, 'b': 4, 'c': 0, 'd': 2, "e": -1, "f": 6}
a = Counter(dic1)
print(a)
#结果:Counter({'b': 4, 'a': 3, 'c': 0, 'e': 0, 'd': -2})
b = Counter(dic2)
print(b)
#结果:Counter({'f': 6, 'b': 4, 'a': 3, 'd': 2, 'c': 0, 'e': -1})
a.subtract(b)
print(a)
#结果:Counter({'e': 1, 'a': 0, 'b': 0, 'c': 0, 'd': -4, 'f': -6})

相关文章

python sort()函数详解 – python列表排序函数

Python3 sorted() 函数 – 对所有可迭代的对象进行排序操作

Python获取两个列表的交集

Python从列表中随机获取元素方法

Python字典按照值(value)的大小进行排序方法

给TA打赏
共{{data.count}}人
人已打赏
Python内置函数Python列表Python笔记

Python sort()函数详解 - Python列表排序函数

2022-7-23 0:39:01

Python异常处理Python笔记玩机技巧

power shell 激活python虚拟环境报错:无法加载文件 *.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 ...

2022-7-30 17:37:20

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索
展开目录