# 使用collections.Counter类统计列表元素出现次数

 from collections import Counter

 names = ["Stanley", "Lily", "Bob", "Well", "Peter", "Bob", "Well", "Peter", "Well", "Peter", "Bob",
"Stanley", "Lily", "Bob", "Well", "Peter", "Bob", "Bob", "Well", "Peter", "Bob", "Well"] names_counts = Counter(names) # 实例化Counter对象,可接收任何hashable序列,Counter对象可以像字典一样访问元素并返回出现的次数
print(names_counts["Stanley"])
#
print(names_counts)
# Counter({'Bob': 7, 'Well': 6, 'Peter': 5, 'Stanley': 2, 'Lily': 2}) top_three = names_counts.most_common(3) # 取出出现次数最多的三个元素
print(top_three)
# [('Bob', 7), ('Well', 6), ('Peter', 5)] more_names = ["Stanley", "Lily", "Bob", "Well"]
names_counts.update(more_names) # 使用update方法新增需要统计的序列
top_three = names_counts.most_common(3) # 取出出现次数最多的三个元素
print(top_three)
# [('Bob', 8), ('Well', 7), ('Peter', 5)] names_counts = Counter(names)
more_names_counts = Counter(more_names)
print(names_counts)
# Counter({'Bob': 7, 'Well': 6, 'Peter': 5, 'Stanley': 2, 'Lily': 2})
print(more_names_counts)
# Counter({'Stanley': 1, 'Lily': 1, 'Bob': 1, 'Well': 1})
print(names_counts - more_names_counts) # 减去次数
# Counter({'Bob': 6, 'Well': 5, 'Peter': 5, 'Stanley': 1, 'Lily': 1})
print(names_counts + more_names_counts) # 合并次数
# Counter({'Bob': 8, 'Well': 7, 'Peter': 5, 'Stanley': 3, 'Lily': 3})

参考资料:
  Python Cookbook, 3rd edition, by David Beazley and Brian K. Jones (O’Reilly).

collections.Counter类统计列表元素出现次数的更多相关文章

  1. [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法

    问题 怎样找出一个序列中出现次数最多的元素呢? 解决方案 collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案 c ...

  2. 用reduce装逼 之 多个数组中得出公共子数组,统计数组元素出现次数

    昨天做了一道美团的面试题,要求是给N个数组,找出N个数组的公共子数组. ,,,,]; ,,,,]; ,,,,]; ,,,,]; 以上四个数组,有公共子数组2, 3,7 function main(){ ...

  3. python之Counter类:计算序列中出现次数最多的元素

    Counter类:计算序列中出现次数最多的元素 from collections import Counter c = Counter('abcdefaddffccef') print('完整的Cou ...

  4. python 数组的操作--统计某个元素在列表中出现的次数

    list.count(obj)  统计某个元素在列表中出现的次数例子: aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for ...

  5. python 列表元素统计出现的次数并输出字典

    import collections my_list = [,,,,,,,,,,,,] print("Original List : ",my_list) ctr = collec ...

  6. python collections中Counter类

    Counter是dict的一个子类,因此具有dict的属性与方法.如常用的iteritems, items, get, pop. class Counter(dict): 如果Key不存在,将返回0, ...

  7. python基础一 ------如何统计一个列表元素的频度

    如何统计一个列表元素的频度 两个需求: 1,统计一个随机序列[1,2,3,4,5,6...]中的出现次数前三的元素及其次数 2,统计一片英文文章中出现次数前10 的单词 两种方法: 1,普通的for循 ...

  8. Python标准库——collections模块的Counter类

    1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...

  9. python统计元素重复次数

    python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...

随机推荐

  1. layui 设计资源——2.0 版本的 Axure 组件包,产品交互设计利器

    大家好,很久不见,这次为大家分享的是 layui_2.0版本的axure组件包,在去年发布的 layui Axure 1.0 中(见:http://fly.layui.com/jie/9842/ )赢 ...

  2. mysql5.7

    原文  http://imysql.com/2015/07/08/mysql-57-new-feature-part-3.shtml 主题 MySQL 本文将和大家一起分享下5.7的新特性,不过我们要 ...

  3. Linux统计文件个数或是代码行数

    统计指定后缀名的文件总个数命令: find . -name *.cpp | wc -l 统计一个目录下代码总行数以及单个文件行数: find . -name *.h | xargs wc -l lin ...

  4. AngularJS中页面传参方法

    1.基于ui-router的页面跳转传参 (1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的 ...

  5. Android(java)学习笔记1:多线程的引入

    1. 多线程的引入:

  6. Django logging的介绍

    Django用的是Python buildin的logging模块. Python logging由四部分组成: Loggers - 记录器 Handles - 处理器 Filters - 过滤器 F ...

  7. grep参数说明及常用用法(转)

    转:https://www.cnblogs.com/leo-li-3046/p/5690613.html grep常用参数说明 grep [OPTIONS] PATTERN [FILE...] gre ...

  8. halcon 分水领域法详解(转载)

    寻思自己写了的,但是这个大佬写的太好了,感谢Y忍冬草_ http://blog.csdn.net/y363703390 https://blog.csdn.net/y363703390/article ...

  9. webpack执行命令失败之解决办法

    错误信息:ERROR in multi ./runoob1.js bundle.js Module not found: Error: Can't resolve 'bundle.js' in 'C: ...

  10. springboot——我的第一个工程

    前言:使用Spring Boot 微服务架构有一段时间了,打算从今天开始记录使用过程. 一.Spring Boot介绍: 简介:Spring Boot 框架的产生,是为了方便我们简化Spring 框架 ...