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中用Ctrl+C终止多线程程序的问题解决
Mar 30 Python
python中使用序列的方法
Aug 03 Python
详解python脚本自动生成需要文件实例代码
Feb 04 Python
Python入门_浅谈for循环、while循环
May 16 Python
解决Python网页爬虫之中文乱码问题
May 11 Python
对python mayavi三维绘图的实现详解
Jan 08 Python
Django组件cookie与session的具体使用
Jun 05 Python
python3实现弹弹球小游戏
Nov 25 Python
python GUI库图形界面开发之PyQt5表单布局控件QFormLayout详细使用方法与实例
Mar 06 Python
使用tensorflow根据输入更改tensor shape
Jun 23 Python
Python Django ORM连表正反操作技巧
Jun 13 Python
Python pyecharts绘制条形图详解
Apr 02 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设计模式之单例模式使用示例
2014/01/20 PHP
PHP输出英文时间日期的安全方法(RFC 1123格式)
2014/06/13 PHP
php curl 获取https请求的2种方法
2015/04/27 PHP
Laravel5.5新特性之友好报错以及展示详解
2017/08/13 PHP
javascript 设计模式之单体模式 面向对象学习基础
2010/04/18 Javascript
js验证模型自我实现的具体方法
2013/06/21 Javascript
JavaScript动态创建div属性和样式示例代码
2013/10/09 Javascript
浅析jquery的js图表组件highcharts
2014/03/06 Javascript
jQuery中replaceWith()方法用法实例
2014/12/25 Javascript
JavaScript数组迭代器实例分析
2015/06/09 Javascript
初步了解javascript面向对象
2015/11/09 Javascript
详解iframe与frame的区别
2016/01/13 Javascript
vue.js 表格分页ajax 异步加载数据
2016/10/18 Javascript
vue2.0数据双向绑定与表单bootstrap+vue组件
2017/02/27 Javascript
mongoose设置unique不生效问题的解决及如何移除unique的限制
2017/11/07 Javascript
Node.js的进程管理的深入理解
2019/01/09 Javascript
HTML元素拖拽功能实现的完整实例
2020/12/04 Javascript
vue el-upload上传文件的示例代码
2020/12/21 Vue.js
使用python调用zxing库生成二维码图片详解
2017/01/10 Python
Python实现动态图解析、合成与倒放
2018/01/18 Python
Python数据类型之Number数字操作实例详解
2019/05/08 Python
Python基于OpenCV实现人脸检测并保存
2019/07/23 Python
Tensorflow的常用矩阵生成方式
2020/01/04 Python
在 Python 中接管键盘中断信号的实现方法
2020/02/04 Python
HTML5中新标签和常用标签详解
2014/03/07 HTML / CSS
洛杉矶健身中心女性专用运动服饰品牌:Marika
2018/05/09 全球购物
彪马日本官网:PUMA日本
2019/01/31 全球购物
加拿大领先的时尚和体育零售商:Sporting Life
2019/12/15 全球购物
Envie de Fraise意大利:法国网上推出的孕妇装品牌
2020/10/18 全球购物
前厅收银主管岗位职责
2014/02/04 职场文书
一体化教学实施方案
2014/05/10 职场文书
科学育儿宣传标语
2014/10/08 职场文书
2015年扫黄打非工作总结
2015/05/13 职场文书
保护环境建议书作文400字
2015/09/14 职场文书
2016高三毕业赠言寄语
2015/12/04 职场文书
vue实现拖拽交换位置
2022/04/07 Vue.js