Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例


Posted in Python onMarch 15, 2018

本文实例讲述了Python找出序列中出现次数最多的元素。分享给大家供大家参考,具体如下:

问题:找出一个元素序列中出现次数最多的元素是什么

解决方案:collections模块中的Counter类正是为此类问题所设计的。它的一个非常方便的most_common()方法直接告诉你答案。

# Determine the most common words in a list
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'
]
from collections import Counter
word_counts = Counter(words)
top_three = word_counts.most_common(3)
print(top_three)
# outputs [('eyes', 8), ('the', 5), ('look', 4)]
# Example of merging in more words
morewords = ['why','are','you','not','looking','in','my','eyes']
word_counts.update(morewords) #使用update()增加计数
print(word_counts.most_common(3))
>>> ================================ RESTART ================================
>>>
[('eyes', 8), ('the', 5), ('look', 4)]
[('eyes', 9), ('the', 5), ('my', 4)]
>>>

在底层实现中,Counter是一个字典,在元素和它们出现的次数间做了映射。

>>> word_counts
Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'why': 1, 'in': 1})
>>> word_counts.most_common(3) #top_three
[('eyes', 9), ('the', 5), ('my', 4)]
>>> word_counts['not']
2
>>> word_counts['eyes']
9
>>> word_counts['eyes']+1
10
>>> word_counts
Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'why': 1, 'in': 1})
>>> word_counts['eyes']=word_counts['eyes']+1 #手动增加元素计数
>>> word_counts
Counter({'eyes': 10, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'why': 1, 'in': 1})
>>>

增加元素出现次数可以通过手动进行增加,也可以借助update()方法;

另外,Counter对象另一个特性是它们可以同各种数学运算操作结合起来使用:

>>> a=Counter(words)
>>> a
Counter({'eyes': 8, 'the': 5, 'look': 4, 'my': 3, 'into': 3, 'around': 2, 'under': 1, "you're": 1, 'not': 1, "don't": 1})
>>> b=Counter(morewords)
>>> b
Counter({'not': 1, 'my': 1, 'in': 1, 'you': 1, 'looking': 1, 'are': 1, 'eyes': 1, 'why': 1})
>>> c=a+b
>>> c
Counter({'eyes': 9, 'the': 5, 'my': 4, 'look': 4, 'into': 3, 'around': 2, 'not': 2, "don't": 1, 'under': 1, 'are': 1, 'looking': 1, "you're": 1, 'you': 1, 'in': 1, 'why': 1})
>>> # substract counts
>>> d=a-b
>>> d
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2, 'under': 1, "you're": 1, "don't": 1})
>>>

当面对任何需要对数据制表或计数的问题时,Counter对象都是你手边的得力工具。比起利用字典自己手写算法,更应采用该方式完成任务。

(代码摘自《Python Cookbook》)

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python易忽视知识点小结
May 25 Python
对numpy 数组和矩阵的乘法的进一步理解
Apr 04 Python
python 列表递归求和、计数、求最大元素的实例
Nov 28 Python
python识别图像并提取文字的实现方法
Jun 28 Python
Python解决pip install时出现的Could not fetch URL问题
Aug 01 Python
Python 日期区间处理 (本周本月上周上月...)
Aug 08 Python
解析python的局部变量和全局变量
Aug 15 Python
python实现微信打飞机游戏
Mar 24 Python
python怎么判断素数
Jul 01 Python
python脚本定时发送邮件
Dec 22 Python
python 爬取豆瓣网页的示例
Apr 13 Python
一劳永逸彻底解决pip install慢的办法
May 24 Python
ubuntu安装sublime3并配置python3环境的方法
Mar 15 #Python
Centos7 Python3下安装scrapy的详细步骤
Mar 15 #Python
python实现word 2007文档转换为pdf文件
Mar 15 #Python
python中使用PIL制作并验证图片验证码
Mar 15 #Python
Python读取Word(.docx)正文信息的方法
Mar 15 #Python
30秒轻松实现TensorFlow物体检测
Mar 14 #Python
tensorflow识别自己手写数字
Mar 14 #Python
You might like
php中使用Akismet防止垃圾评论的代码
2011/06/10 PHP
PHP无刷新上传文件实现代码
2011/09/19 PHP
php使用CURL伪造IP和来源实例详解
2015/01/15 PHP
PHP生成唯一订单号的方法汇总
2015/04/16 PHP
Linux安装配置php环境的方法
2016/01/14 PHP
100行PHP代码实现socks5代理服务器
2016/04/28 PHP
总结对比php中的多种序列化
2016/08/28 PHP
php 二维数组时间排序实现代码
2016/11/19 PHP
PHP __call()方法实现委托示例
2019/05/20 PHP
用正则表达式替换图片地址img标签
2013/11/22 Javascript
Underscore.js 1.3.3 中文注释翻译说明
2015/06/25 Javascript
JS实现超精简响应鼠标显示二级菜单代码
2015/09/12 Javascript
Javascript编程之继承实例汇总
2015/11/28 Javascript
浅析Bootstrap缩略图组件与警示框组件
2016/04/29 Javascript
对Angular.js Controller如何进行单元测试
2016/10/25 Javascript
jquery pagination插件动态分页实例(Bootstrap分页)
2016/12/23 Javascript
Vue.js 2.0学习教程之从基础到组件详解
2017/04/24 Javascript
JavaScript你不知道的一些数组方法
2017/08/18 Javascript
解决element-ui中下拉菜单子选项click事件不触发的问题
2018/08/22 Javascript
vue图片上传组件使用详解
2019/12/23 Javascript
Vue项目中使用flow做类型检测的方法
2020/03/18 Javascript
解决vue做详情页跳转的时候使用created方法 数据不会更新问题
2020/07/24 Javascript
python多线程http下载实现示例
2013/12/30 Python
pygame学习笔记(2):画点的三种方法和动画实例
2015/04/15 Python
Python连接PostgreSQL数据库的方法
2016/11/28 Python
使用 Python 实现微信公众号粉丝迁移流程
2018/01/03 Python
在SQLite-Python中实现返回、查询中文字段的方法
2019/07/17 Python
Django发送邮件和itsdangerous模块的配合使用解析
2019/08/10 Python
python opencv调用笔记本摄像头
2019/08/28 Python
CSS3 完美实现圆角效果
2009/07/13 HTML / CSS
基于zepto的插件之移动端无缝向上滚动并上下触摸滑动实例代码
2016/12/20 HTML / CSS
台湾时尚彩瞳专门店:imeime
2019/08/16 全球购物
工商企业管理实习自我鉴定
2013/12/04 职场文书
致铅球运动员加油稿
2014/02/13 职场文书
网上祭先烈心得体会
2014/09/01 职场文书
教师作风建设剖析材料
2014/10/11 职场文书