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(Tornado)模拟登录小米抢手机
Nov 12 Python
python异步任务队列示例
Apr 01 Python
编写Python脚本来获取Google搜索结果的示例
May 04 Python
Python获取linux主机ip的简单实现方法
Apr 18 Python
Python断言assert的用法代码解析
Feb 03 Python
python将三维数组展开成二维数组的实现
Nov 30 Python
python 读取更新中的log 或其它文本方式
Dec 24 Python
基于Tensorflow使用CPU而不用GPU问题的解决
Feb 07 Python
matplotlib subplot绘制多个子图的方法示例
Jul 28 Python
浅析PyCharm 的初始设置(知道)
Oct 12 Python
ffmpeg+Python实现B站MP4格式音频与视频的合并示例代码
Oct 21 Python
python分分钟绘制精美地图海报
Feb 15 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
电脑硬件及电脑配置知识大全
2020/03/17 数码科技
Laravel框架中Blade模板的用法示例
2017/08/30 PHP
php mysql PDO 查询操作的实例详解
2017/09/23 PHP
jQuery的实现原理的模拟代码 -3 事件处理
2010/08/03 Javascript
Jquery replace 字符替换实现代码
2010/12/02 Javascript
javascript中关于break,continue的特殊用法与介绍
2012/05/24 Javascript
jQuery 拖动层(在可视区域范围内)
2012/05/24 Javascript
javascript与有限状态机详解
2014/05/08 Javascript
jQuery使用after()方法在元素后面添加多项内容的方法
2015/03/26 Javascript
JavaScript性能优化之小知识总结
2015/11/20 Javascript
微信小程序 同步请求授权的详解
2017/08/04 Javascript
详解Vue+axios+Node+express实现文件上传(用户头像上传)
2018/08/10 Javascript
Angular动态绑定样式及改变UI框架样式的方法小结
2018/09/03 Javascript
webpack 静态资源集中输出的方法示例
2018/11/09 Javascript
vuejs简单验证码功能完整示例
2019/01/08 Javascript
electron-vue开发环境内存泄漏问题汇总
2019/10/10 Javascript
微信小程序实现点击生成随机验证码
2020/09/09 Javascript
nodejs+express最简易的连接数据库的方法
2020/12/23 NodeJs
利用Vue实现简易播放器的完整代码
2020/12/30 Vue.js
python目录操作之python遍历文件夹后将结果存储为xml
2014/01/27 Python
Python虚拟环境virtualenv的安装与使用详解
2017/05/28 Python
Python网络爬虫神器PyQuery的基本使用教程
2018/02/03 Python
[原创]Python入门教程1. 基本运算【四则运算、变量、math模块等】
2018/10/28 Python
[原创]Python入门教程4. 元组基本操作
2018/10/31 Python
python创建属于自己的单词词库 便于背单词
2019/07/30 Python
详解python uiautomator2 watcher的使用方法
2019/09/09 Python
python DataFrame转dict字典过程详解
2019/12/26 Python
2013年入党人员的自我鉴定
2013/10/25 职场文书
幼儿园园长岗位职责
2013/11/26 职场文书
农村婚礼主持词
2014/03/13 职场文书
信息合作协议书
2014/10/09 职场文书
国际贸易实训报告
2014/11/05 职场文书
幼儿园开学通知
2015/04/24 职场文书
夫妻吵架保证书
2015/05/08 职场文书
《少年闰土》教学反思
2016/02/18 职场文书
Linux系统下MySQL配置主从分离的步骤
2022/03/21 MySQL