[PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法
问题
怎样找出一个序列中出现次数最多的元素呢?
解决方案
collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案
collections.Counter 类
1. most_common(n)统计top_n
from collections import Counter
words = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
#创建Counter对象
word_counts=Counter(words) #most_common(n)函数可直接统计top-n
top_three=word_counts.most_common(3) print(type(top_three))
<class 'list'> print(top_three)
[('eyes', 8), ('the', 5), ('look', 4)]
2. 统计任意元素出现的次数
Counter 对象可以接受任意的由可哈希(hashable)元素构成的序列对象
在底层实现上,一个 Counter 对象就是一个字典,将元素映射到它出现的次数上
print(word_counts['look'])
4
print(word_counts["you're"])
1
3. 两个Counter对象之间可以互相使用数学运算符,从而叠加/叠减统计
word1 = [
'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
'my', 'eyes', "you're", 'under'
]
word2 = ['why','are','you','not','looking','in','my','eyes'] a=Counter(word1)
b=Counter(word2)
print(a)
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2, 'not': 1, 'under': 1, "you're": 1, "don't": 1}) print(b)
Counter({'eyes': 1, 'not': 1, 'are': 1, 'you': 1, 'in': 1, 'why': 1, 'my': 1, 'looking': 1}) print(a+b)
Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, 'under': 1, 'looking': 1, 'you': 1, 'why': 1, 'in': 1, 'are': 1, "you're": 1, "don't": 1}) print(a-b)
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'around': 2, 'my': 2, 'under': 1, "you're": 1, "don't": 1})
[PY3]——找出一个序列中出现次数最多的元素/collections.Counter 类的用法的更多相关文章
- Problem A: 零起点学算法91——找出一个数组中出现次数最多的那个元素
#include<stdio.h> int main() { ],b[]={}; while(scanf("%d",&n)!=EOF) { ;i<n;i+ ...
- 【python cookbook】【数据结构与算法】12.找出序列中出现次数最多的元素
问题:找出一个元素序列中出现次数最多的元素是什么 解决方案:collections模块中的Counter类正是为此类问题所设计的.它的一个非常方便的most_common()方法直接告诉你答案. # ...
- python之Counter类:计算序列中出现次数最多的元素
Counter类:计算序列中出现次数最多的元素 from collections import Counter c = Counter('abcdefaddffccef') print('完整的Cou ...
- 【python cookbook】找出序列中出现次数最多的元素
问题 <Python Cookbook>中有这么一个问题,给定一个序列,找出该序列出现次数最多的元素.例如: words = [ 'look', 'into', 'my', 'eyes', ...
- 在线性级别时间内找出无序序列中的第k个元素
在一个无序序列中找出第k个元素,对于k很小或者很大时可以采取特殊的方法,比如用堆排序来实现 .但是对于与序列长度N成正比的k来说,就不是一件容易的事了,可能最容易想到的就是先将无序序列排序再遍历即可找 ...
- python 找出一篇文章中出现次数最多的10个单词
#!/usr/bin/python #Filename: readlinepy.py import sys,re urldir=r"C:\python27\a.txt" disto ...
- 笔试题&面试题:找出一个数组中第m小的值并输出
题目:找出一个数组中第m小的值并输出. 代码: #include <stdio.h> int findm_min(int a[], int n, int m) //n代表数组长度,m代表找 ...
- Python中用max()筛选出列表中出现次数最多的元素
1 List = [1,2,3,4,2,3,2] # 随意创建一个只有数字的列表 2 maxTimes = max(List,key=List.count) # maxTimes指列表中出现次数最多的 ...
- javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数
javascript 写一段代码,判断一个字符串中出现次数最多的字符串,并统计出现的次数 function test(){ var bt = document.getElementById(" ...
随机推荐
- What is the difference between inverse converse and reverse?
http://wiki.answers.com/Q/What_is_the_difference_between_inverse_converse_and_reverse First, it help ...
- RobotFramework与Jenkins集成发送邮件
转: A. 目标:实现RobotFramework的脚本定时自动执行,执行完后自动将结果发送到指定邮箱 B. 前提1. 配置好Robot Framework的环境,脚本可以正常运行 2. ...
- leetcode 从排序数组中删除重复项
最近的学习是相当的无聊,并且很无趣,每天都浪费了很多时间,比如今天下午,就是搞一手成语接龙,我也是醉了- 并且我也不知道学什么了,所以决定刷题 虽然我是0算法基础,0逻辑能力的渣渣,但是尽力每天做一道 ...
- ZKEACMS 模板组件扩展
前言 如果你还不知道ZKEACMS,不妨先了解一下. ASP.NET MVC 开源建站系统 ZKEACMS 推荐,从此网站“拼”起来 官方地址:http://www.zkea.net/zkeacms ...
- JFrog Artifactory
是一款二进制存储管理工具,用来管理构建构建工具(如:gradle.maven.nuget.docker等等)等所依赖的二进制仓库,以方便管理第三方库和发布目标版本库,从而提高软件开发效率. 为DevO ...
- VSM(Virtual Storage Manager) add new osd 代码分析
vsm add new osd 流程 后台获取可用设备 | 选择可用设备及osd相关信息等 | 点击add按钮,把准备部署的osd信息添加到需要部署的item列表中 | 点击submit按钮,添加it ...
- 在Python程序中调用Java代码的实现
<原创不易,转载请标明出处:https://www.cnblogs.com/bandaobudaoweng/p/10785766.html> 前言 开发Python程序,需求中需要用到Ja ...
- 洛谷P5050 【模板】多项式多点求值
传送门 人傻常数大.jpg 因为求逆的时候没清零结果调了几个小时-- 前置芝士 多项式除法,多项式求逆 什么?你不会?左转你谷模板区,包教包会 题解 首先我们要知道一个结论\[f(x_0)\equiv ...
- ubuntu和centos安装docker
一. UBUNTU系统上 1. 卸载旧版本(新系统不用执行) sudo apt-get remove docker docker-engine docker.io 2. 安装docker st ...
- 「案例」重新设计 Adobe 的文件类型图标
Adobe 的品牌设计团队负责为公司旗下桌面端.移动端和 web 端的产品进行品牌设计.品牌元素的形式很多,可以是两个字母的产品 logo,应用启动界面,产品里的图标等等. 一个很常见却常被忽视的品牌 ...