python中counter_Python collections模块中counter()的详细说明,Pythoncollections,之,Counter,详解...

collections模块 ==> Python标准库,数据结构常用的模块;collections包含了一些特殊的容器,针对Python内置的容器,例如list、dict、set和tuple,提供了另一种选择。

collections模块常用类型有:

计数器(

Counter

)

dict的子类,计算可hash的对象

请点击

Counter

双向队列(

deque

)

类似于list的容器,可以快速的在队列头部和尾部添加、删除元素

请点击

deque

默认字典(defaultdict)

dict的子类,可以调用提供默认值的函数

有序字典(OrderedDict)

dict的子类,可以记住元素的添加顺序

可命名元组(namedtuple)

可以创建包含名称的tuple

Counter()

主要功能:可以支持方便、快速的计数,将元素数量统计,然后计数并返回一个字典,键为元素,值为元素个数。

from collections import Counter

list1 = ["a", "a", "a", "b", "c", "c", "f", "g", "g", "g", "f"]

dic = Counter(list1)

print(dic)

#结果:次数是从高到低的

#Counter({'a': 3, 'g': 3, 'c': 2, 'f': 2, 'b': 1})

print(dict(dic))

#结果:按字母顺序排序的

#{'a': 3, 'b': 1, 'c': 2, 'f': 2, 'g': 3}

print(dic.items()) #dic.items()获取字典的key和value

#结果:按字母顺序排序的

#dict_items([('a', 3), ('b', 1), ('c', 2), ('f', 2), ('g', 3)])

print(dic.keys())

#结果:

#dict_keys(['a', 'b', 'c', 'f', 'g'])

print(dic.values())

#结果:

#dict_values([3, 1, 2, 2, 3])

print(sorted(dic.items(), key=lambda s: (-s[1])))

#结果:按统计次数降序排序

#[('a', 3), ('g', 3), ('c', 2), ('f', 2), ('b', 1)]

for i, v in dic.items():

if v == 1:

print(i)

#结果:

#b

from collections import Counter

str1 = "aabbfkrigbgsejaae"

print(Counter(str1))

print(dict(Counter(str1)))

#结果:

#Counter({'a': 4, 'b': 3, 'g': 2, 'e': 2, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 's': 1, 'j': 1})

#{'a': 4, 'b': 3, 'f': 1, 'k': 1, 'r': 1, 'i': 1, 'g': 2, 's': 1, 'e': 2, 'j': 1}

dic1 = {'a': 3, 'b': 4, 'c': 0, 'd': -2}

print(Counter(dic1))

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()))

#结果:aaabdzabcffccgggc1a1111000b10

print(''.join(list2))

#结果: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})

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值