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 相关文章推荐
python3.5 tkinter实现页面跳转
Jan 30 Python
python 日志增量抓取实现方法
Apr 28 Python
tensorflow 中对数组元素的操作方法
Jul 27 Python
在Python中将函数作为另一个函数的参数传入并调用的方法
Jan 22 Python
Python实现的删除重复文件或图片功能示例【去重】
Apr 23 Python
Python3.5常见内置方法参数用法实例详解
Apr 29 Python
Django model update的多种用法介绍
Mar 28 Python
Python使用ffmpy将amr格式的音频转化为mp3格式的例子
Aug 08 Python
python使用socket实现的传输demo示例【基于TCP协议】
Sep 24 Python
python实现机器人卡牌
Oct 06 Python
关于Tensorflow使用CPU报错的解决方式
Feb 05 Python
python thrift 实现 单端口多服务的过程
Jun 08 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程序开发范例学习之表单 获取文本框的值
2011/08/08 PHP
PHP实现文件上传和多文件上传
2015/12/24 PHP
PHP连接MSSQL方法汇总
2016/02/05 PHP
PHP+mysql实现从数据库获取下拉树功能示例
2017/01/06 PHP
laravel7学习之无限级分类的最新实现方法
2020/09/30 PHP
PHP网站常见安全漏洞,及相应防范措施总结
2021/03/01 PHP
ThinkPHP5.1的权限控制怎么写?分享一个AUTH权限控制
2021/03/09 PHP
一个判断email合法性的函数[非正则]
2008/12/09 Javascript
Jquery公告滚动+AJAX后台得到数据
2011/04/14 Javascript
jQuery实现form表单reset按钮重置清空表单功能
2012/12/18 Javascript
JQuery for与each性能比较分析
2013/05/14 Javascript
javascript中的Function.prototye.bind
2015/06/25 Javascript
浅析jquery unbind()方法移除元素绑定的事件
2016/05/24 Javascript
利用fecha进行JS日期处理
2016/11/21 Javascript
AngularJS ng-repeat指令及Ajax的应用实例分析
2017/07/06 Javascript
echarts统计x轴区间的数值实例代码详解
2019/07/07 Javascript
[55:02]2014 DOTA2国际邀请赛中国区预选赛 HGT VS Orenda
2014/05/21 DOTA
python实现的一只从百度开始不断搜索的小爬虫
2013/08/13 Python
解决python3中自定义wsgi函数,make_server函数报错的问题
2017/11/21 Python
Python中list的交、并、差集获取方法示例
2019/08/01 Python
python异步编程 使用yield from过程解析
2019/09/25 Python
python集合的创建、添加及删除操作示例
2019/10/08 Python
调用其他python脚本文件里面的类和方法过程解析
2019/11/15 Python
python实现ftp文件传输功能
2020/03/20 Python
如何在django中运行scrapy框架
2020/04/22 Python
python3.6环境下安装freetype库和基本使用方法(推荐)
2020/05/10 Python
Anaconda详细安装步骤图文教程
2020/11/12 Python
利用python进行文件操作
2020/12/04 Python
物流经理自我评价
2013/09/23 职场文书
研发工程师的岗位职责
2013/11/18 职场文书
企业精细化管理实施方案
2014/03/23 职场文书
春节晚会主持词
2014/03/24 职场文书
放飞中国梦演讲稿
2014/04/23 职场文书
2014年大学教师工作总结
2014/12/02 职场文书
MySQL实战记录之如何快速定位慢SQL
2022/03/23 MySQL
MySQL的意向共享锁、意向排它锁和死锁
2022/07/15 MySQL