Python collections中的Counter

参考文章:https://blog.csdn.net/Shiroh_ms08/article/details/52653385

一、collections整体介绍

collections:高性能容器的数据类型

在2.4版本中新加入,源代码Lib/collections.pyLib/_abcoll.py。该模块实现了专用的容器数据类型来替代python的通用内置容器:dict(字典),list(列表), set(集合)和tuple(元组)。

容器 描述 引入版本
namedtuple() 使用工厂方法创建带有命名的字段的元组的子类 2.6
deque 类似列表的容器,能够快速响应在任何一端进行pop 2.4
Counter 字典子类,为可以进行哈希的对象计数 2.7
OrderedDict 字典子类,记录了字典的添加次序 2.7
defaultdict 字典子类,调用一个工厂方法来提供缺失的值 2.5

除了具体的容器类,collections模块还提供了abstract_base_classes来测试一个类是否体用了一个特定的接口,例如,这是可哈希的还是一个映射。

二、Counter作用及举例

counter工具用于支持便捷和快速地计数。

1、简单示例

from collections import Counter
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
    cnt[word] += 1
print cnt

输出为

Counter({'blue': 3, 'red': 2, 'green': 1})

2、影评中的正负评价高频词统计

 
 
  1. from collections import Counter
  2. positive_counts = Counter()
  3. negative_counts = Counter()
  4. total_counts = Counter()
  5. for i in range(len(reviews)):
  6. if(labels[i] == 'POSITIVE'):
  7. for word in reviews[i].split(" "):
  8. positive_counts[word] += 1
  9. total_counts[word] += 1
  10. else:
  11. for word in reviews[i].split(" "):
  12. negative_counts[word] += 1
  13. total_counts[word] += 1
  14. positive_counts.most_common()
  15. negative_counts.most_common()


猜你喜欢

转载自blog.csdn.net/weixin_41770169/article/details/80082760