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生成验证码图片代码分享
Jan 28 Python
详解常用查找数据结构及算法(Python实现)
Dec 09 Python
python使用matplotlib绘制柱状图教程
Feb 08 Python
python中的随机函数random的用法示例
Jan 27 Python
tensorflow 使用flags定义命令行参数的方法
Apr 23 Python
Python基于多线程实现抓取数据存入数据库的方法
Jun 22 Python
Centos下实现安装Python3.6和Python2共存
Aug 15 Python
Python使用while循环花式打印乘法表
Jan 28 Python
Flask框架请求钩子与request请求对象用法实例分析
Nov 07 Python
python re模块匹配贪婪和非贪婪模式详解
Feb 11 Python
tensorflow pb to tflite 精度下降详解
May 25 Python
pandas求平均数和中位数的方法实例
Aug 04 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+XML 制作简单的留言本 图文教程
2009/11/02 PHP
PHP提交表单失败后如何保留已经填写的信息
2014/06/20 PHP
PHP实现时间比较和时间差计算的方法示例
2017/07/24 PHP
YII框架学习笔记之命名空间、操作响应与视图操作示例
2019/04/30 PHP
Js+Dhtml:WEB程序员简易开发工具包(预先体验版)
2006/11/07 Javascript
jQuery 位置插件
2008/12/25 Javascript
jQuery 树形结构的选择器
2010/02/15 Javascript
innerHTML 和 getElementsByName 在IE下面的bug 的解决
2010/04/09 Javascript
基于Jquery+Ajax+Json的高效分页实现代码
2011/10/29 Javascript
JavaScript使用shift方法移除素组第一个元素实例分析
2015/04/06 Javascript
vue.js组件之间传递数据的方法
2017/07/10 Javascript
js保留两位小数方法总结
2018/01/31 Javascript
微信公众号生成新浪短网址的实现(快速生成)
2019/08/18 Javascript
微信小程序实现时间进度条功能
2020/11/17 Javascript
vue输入框使用模糊搜索功能的实现代码
2020/05/26 Javascript
[54:33]2018DOTA2亚洲邀请赛小组赛 A组加赛 Liquid vs Optic
2018/04/03 DOTA
Python中的fileinput模块的简单实用示例
2015/07/09 Python
python 判断是否为正小数和正整数的实例
2017/07/23 Python
python导出hive数据表的schema实例代码
2018/01/22 Python
Python之批量创建文件的实例讲解
2018/05/10 Python
解决nohup执行python程序log文件写入不及时的问题
2019/01/14 Python
给我一面国旗 python帮你实现
2019/09/30 Python
python 读取更新中的log 或其它文本方式
2019/12/24 Python
Django Admin 上传文件到七牛云的示例代码
2020/06/20 Python
pytorch VGG11识别cifar10数据集(训练+预测单张输入图片操作)
2020/06/24 Python
文化与传播毕业生求职信
2014/03/09 职场文书
保险公司晨会主持词
2014/03/22 职场文书
母亲节演讲稿
2014/05/27 职场文书
北京故宫导游词
2015/01/31 职场文书
应届生求职自荐信范文
2015/03/04 职场文书
学校通报表扬范文
2015/05/04 职场文书
纪念建国70周年演讲稿
2019/07/19 职场文书
nginx的zabbix 5.0安装部署的方法步骤
2021/07/16 Servers
MySQL常见优化方案汇总
2022/01/18 MySQL
Python通过loop.run_in_executor执行同步代码 同步变为异步
2022/04/11 Python
JavaScript前端面试扁平数据转tree与tree数据扁平化
2022/06/14 Javascript