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 = [ ...
随机推荐
- sql语句查询一个表里面无重复并且按照指定字段排序的sql语句
SELECT a.* FROM product_template a INNER JOIN (SELECT p_id,MAX(ID) as max_id FROM product_template w ...
- [原]Linux 命令行 发送邮件
1.mail -s hi xx@yy.com 给xx@yy.com发一封主题为hi的信(没有正文) 编辑完内容后Ctrl-D结束. 2.echo "This is a test mail!& ...
- SAP CRM系统订单模型的设计与实现
SAP成都研究院的一个部门领导让我给他的团队做一个SAP CRM One Order框架的培训,这是我准备的培训内容. 在Jerry之前的文章 基于SAP Kyma的订单编排增强介绍,我表达了自己对S ...
- SAP Fiori + Vue = ?
2017年3月28日,我到国内一个SAP CRM客户那里,同他们的架构师关于二次开发的UI框架选择SAP UI5还是Vue进行了一番探讨.回到SAP研究院之后,我把这个问题扔到了公司的微信群里,引起了 ...
- 笔记,记事软件(RedbookNote, lifeopraph)
许多人重视记日记是因为它是一种以天为基础保存个人或商务信息的良好方式:持续跟踪每天的生活和思想上的点点滴滴,组织和巩固记忆.思考.商业交易.电子邮件.账单.未来计划.联系人列表,甚至是秘密信息.Lin ...
- UVa 12265 - Selling Land
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- shiro密码的比对,密码的MD5加密,MD5盐值加密,多个Relme
有具体问题的可以参考之前的关于shiro的博文,关于shiro的博文均是一次工程的内容 密码的比对 通过AuthenticatingRealm的CredentialsMatcher方法 密码的加密 ...
- 关于Git学习推荐
Git学习除了推荐官方网站:https://git-scm.com/之外, 我个人比较推荐初学者或者被动使用者可以学习参考廖雪峰的这个教程:https://www.liaoxuefeng.com/wi ...
- Redis(RedisTemplate)使用string字符串
RedisTemplate配置:https://www.cnblogs.com/weibanggang/p/10188682.html ApplicationContext applicationCo ...
- Gradle Goodness: Profiling Information
If we want to know more about how much time is spent in tasks we can use the --profile command-line ...