什么是Counter

Python collections.Counter用法

什么是collections

collections在python官方文档中的解释是High-performance container datatypes,直接的中文翻译解释高性能容量数据类型
它总共包含五种数据类型:
在这里插入图片描述
其中Counter中文意思是计数器,也就是我们常用于统计的一种数据类型,在使用Counter之后可以让我们的代码更加简单易读。

Counter

我们先看一个简单的例子:

#统计词频
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
result = {}
for color in colors:
    if result.get(color)==None:
        result[color]=1
    else:
        result[color]+=1
print (result)
#{'red': 2, 'blue': 3, 'green': 1}

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

下面我们看用Counter怎么实现:

from collections import Counter
colors = ['red', 'blue', 'red', 'green', 'blue', 'blue']
c = Counter(colors)
print (dict(c))

   
   
  • 1
  • 2
  • 3
  • 4

显然代码更加简单了,也更容易读和维护了。

Counter操作

可以创建一个空的Counter:

cnt = Counter()

   
   
  • 1

之后在空的Counter上进行一些操作。
也可以创建的时候传进去一个迭代器(数组,字符串,字典等):

c = Counter('gallahad')                 # 传进字符串
c = Counter({'red': 4, 'blue': 2})      # 传进字典
c = Counter(cats=4, dogs=8)             # 传进元组

   
   
  • 1
  • 2
  • 3

判断是否包含某元素,可以转化为dict然后通过dict判断,Counter也带有函数可以判断:

c = Counter(['eggs', 'ham'])
c['bacon']                              # 不存在就返回0
#0

   
   
  • 1
  • 2
  • 3

删除元素:

c['sausage'] = 0                        # counter entry with a zero count
del c['sausage']   

   
   
  • 1
  • 2

获得所有元素:

c = Counter(a=4, b=2, c=0, d=-2)
list(c.elements())
#['a', 'a', 'a', 'a', 'b', 'b']

   
   
  • 1
  • 2
  • 3

查看最常见出现的k个元素:

Counter('abracadabra').most_common(3)
#[('a', 5), ('r', 2), ('b', 2)]

   
   
  • 1
  • 2

Counter更新:

c = Counter(a=3, b=1)
d = Counter(a=1, b=2)
c + d                       # 相加
#Counter({'a': 4, 'b': 3})
c - d                       # 相减,如果小于等于0,删去
#Counter({'a': 2})
c & d                       # 求最小
#Counter({'a': 1, 'b': 1})
c | d                       # 求最大
#Counter({'a': 3, 'b': 2})

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

例子

例子:读文件统计词频并按照出现次数排序,文件是以空格隔开的单词的诸多句子:

from collections import Counter
lines = open("./data/input.txt","r").read().splitlines()
lines = [lines[i].split(" ") for i in range(len(lines))]
words = []
for line in lines:
    words.extend(line)
result = Counter(words)
print (result.most_common(10))

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

当需要统计的文件比较大,使用read()一次读不完的情况:

from collections import Counter
result = Counter()
with open("./data/input.txt","r") as f:
    while True:
        lines = f.read(1024).splitlines()
        if lines==[]:
            break
        lines = [lines[i].split(" ") for i in range(len(lines))]
        words = []
        for line in lines:
            words.extend(line)
        tmp = Counter(words)
        result+=tmp

print (result.most_common(10))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

具体可以参考 https://docs.python.org/2/library/collections.html#collections.Counter.most_common

原文链接:https://blog.csdn.net/qwe1257/article/details/83272340
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值