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 判断一个进程是否存在
Apr 09 Python
python cookielib 登录人人网的实现代码
Dec 19 Python
用Python的线程来解决生产者消费问题的示例
Apr 02 Python
Python urllib、urllib2、httplib抓取网页代码实例
May 09 Python
Python操作串口的方法
Jun 17 Python
Python输出汉字字库及将文字转换为图片的方法
Jun 04 Python
python 使用get_argument获取url query参数
Apr 28 Python
Python基于Floyd算法求解最短路径距离问题实例详解
May 16 Python
python得到电脑的开机时间方法
Oct 15 Python
Python IDLE或shell中切换路径的操作
Mar 09 Python
记一次django内存异常排查及解决方法
Aug 07 Python
pycharm最新激活码有效期至2100年(亲测可用)
Feb 05 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调用Oracle存储过程的方法
2008/09/12 PHP
php实现12306火车票余票查询和价格查询(12306火车票查询)
2014/01/14 PHP
php操作access数据库的方法详解
2017/02/22 PHP
PHP+MySQL实现在线测试答题实例
2020/01/02 PHP
详解phpstorm2020最新破解方法
2020/09/17 PHP
半角全角相互转换的js函数
2009/10/16 Javascript
JQuery+CSS提示框实现思路及代码(纯手工打造)
2013/05/07 Javascript
jquery实现类似淘宝星星评分功能实例
2014/09/12 Javascript
JS+CSS实现可拖动的弹出提示框
2015/02/16 Javascript
ECMAScript中函数function类型
2015/06/03 Javascript
jQuery实现鼠标跟随提示层效果代码(可显示文本,Div,Table,Html等)
2016/04/18 Javascript
将angular.js项目整合到.net mvc中的方法详解
2017/06/29 Javascript
通过命令行生成vue项目框架的方法
2017/07/12 Javascript
vue动态删除从数据库倒入列表的某一条方法
2018/09/29 Javascript
微信公众平台获取access_token的方法步骤
2019/03/29 Javascript
layer.alert回调函数执行关闭弹窗的实例
2019/09/11 Javascript
[02:04]2014DOTA2国际邀请赛 BBC小组赛第三天总结
2014/07/12 DOTA
使用python BeautifulSoup库抓取58手机维修信息
2013/11/21 Python
Python tempfile模块学习笔记(临时文件)
2014/05/25 Python
Python模块包中__init__.py文件功能分析
2016/06/14 Python
Ubuntu安装Jupyter Notebook教程
2017/10/18 Python
Python迭代器和生成器定义与用法示例
2018/02/10 Python
基于python实现名片管理系统
2018/11/30 Python
python将txt文件读取为字典的示例
2018/12/22 Python
python 批量解压压缩文件的实例代码
2019/06/27 Python
Python+unittest+requests+excel实现接口自动化测试框架
2020/12/23 Python
使用HTML5原生对话框元素并轻松创建模态框组件
2019/03/06 HTML / CSS
NEW LOOK官网:英国时装零售巨头之一,快时尚品牌
2017/01/11 全球购物
一套比较完整的软件测试人员面试题
2012/05/13 面试题
计算机专业毕业生自我鉴定
2014/01/16 职场文书
2014财务部年度工作总结
2014/12/08 职场文书
2015年学校总务处工作总结
2015/05/19 职场文书
python通过函数名调用函数的几种方法总结
2021/06/07 Python
Golang并发操作中常见的读写锁详析
2021/08/30 Golang
Python Django获取URL中的数据详解
2021/11/01 Python
新的CSS 伪类函数 :is() 和 :where()示例详解
2022/08/05 HTML / CSS