collections.Counter类统计列表元素出现次数
# 使用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类统计列表元素出现次数的更多相关文章
- [PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
问题 怎样找出一个序列中出现次数最多的元素呢? 解决方案 collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案 c ...
- 用reduce装逼 之 多个数组中得出公共子数组,统计数组元素出现次数
昨天做了一道美团的面试题,要求是给N个数组,找出N个数组的公共子数组. ,,,,]; ,,,,]; ,,,,]; ,,,,]; 以上四个数组,有公共子数组2, 3,7 function main(){ ...
- python之Counter类:计算序列中出现次数最多的元素
Counter类:计算序列中出现次数最多的元素 from collections import Counter c = Counter('abcdefaddffccef') print('完整的Cou ...
- python 数组的操作--统计某个元素在列表中出现的次数
list.count(obj) 统计某个元素在列表中出现的次数例子: aList = [123, 'xyz', 'zara', 'abc', 123]; print "Count for ...
- python 列表元素统计出现的次数并输出字典
import collections my_list = [,,,,,,,,,,,,] print("Original List : ",my_list) ctr = collec ...
- python collections中Counter类
Counter是dict的一个子类,因此具有dict的属性与方法.如常用的iteritems, items, get, pop. class Counter(dict): 如果Key不存在,将返回0, ...
- python基础一 ------如何统计一个列表元素的频度
如何统计一个列表元素的频度 两个需求: 1,统计一个随机序列[1,2,3,4,5,6...]中的出现次数前三的元素及其次数 2,统计一片英文文章中出现次数前10 的单词 两种方法: 1,普通的for循 ...
- Python标准库——collections模块的Counter类
1.collections模块 collections模块自Python 2.4版本开始被引入,包含了dict.set.list.tuple以外的一些特殊的容器类型,分别是: OrderedDict类 ...
- python统计元素重复次数
python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...
随机推荐
- java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]. Specify @BootstrapWith's 'value' attribute or make the default bootstrapper class available.
1.前几天搭建单元测后,今天用其测试,结果报了这个问题.网上搜索后,刚开始以为原因是 Spring的 依赖版本的问题,我现在的依赖是: 因为其他的比如说 spring-content spring ...
- 【转】msyql使用-用户创建/权限配置
MySQL 默认有个root用户,但是这个用户权限太大,一般只在管理数据库时候才用.如果在项目中要连接 MySQL 数据库,则建议新建一个权限较小的用户来连接. 在 MySQL 命令行模式下输入如下命 ...
- LeetCode-Largest Rectangle in Histogram O(n) 详析-ZZ
http://www.cnblogs.com/felixfang/p/3676193.html Largest Rectangle in Histogram Given n non-negative ...
- Vc code
通达信L2行情接口, 包括普通行情接口所有功能, 并新增了十档行情, 逐笔成交,买卖队列和深圳逐笔委托(上海证券交易所不对外提供逐笔委托数据)这四个新功能. // TdxHqDemoCpp.cpp : ...
- 【转】Linux中/etc/profile,/etc/bashrc,~/.profile,~/.bashrcd的区别
//因为在原文章中博主说以下内容是网友说的,所以我就只加个转了,找不到原作者 /etc/profile,/etc/bashrc 是系统全局环境变量设定 ~/.profile,~/.bashrc用户家目 ...
- C++默认值。
类成员变量: char :� //随机int :2685372 //随机string :size:0length:0 vector :0map :0 结构体 //取决于成员自己的类型 类成员静态变量: ...
- EOF及相关函数
结论:EOF是在头文件stdio.h中预定义的一个宏,而eof(end of file)是一个与标准输入/输出流相关联的标志位.当文件指针已经指向文件尾且再次尝试读取时,eof标志会被设置.同时,某些 ...
- 阶&原根
求阶的方法: 根据性质2,直接对ϕ(m)求出因子即可,从小到大依次判断是不是符合ad = 1(mod m)(d是ϕ(m)的因子) 求最小的原根的方法: 根据性质8,对ϕ(m)求出素因子,从1开始不断测 ...
- 安装MySql-Python遇到的错误及解决方法
用pip安装mysql-python时报错: _mysql.c _mysql.c(42) : fatal error C1083: Cannot open include file: 'config- ...
- 如何设置活动监视器中的可见作业数能显示更长范围之内的作业(如何让bpdbjobs/Active Monitor显示更多作业信息)
一.问题: 如何设置可以使得活动监视器中的可见作业数能显示更长范围之内的作业(即NBU软件不要自动删除活动监视器中短时间内(如8天)内的作业记录)? 二.解决方法: 默认情况下在Ac ...