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 EOL while scanning string literal问题解决方法
Sep 18 Python
在DigitalOcean的服务器上部署flaskblog应用
Dec 19 Python
浅谈python中的数字类型与处理工具
Aug 02 Python
Python3爬楼梯算法示例
Mar 04 Python
谈一谈基于python的面向对象编程基础
May 21 Python
python实现倒计时小工具
Jul 29 Python
python excel转换csv代码实例
Aug 26 Python
使用OpenCV实现仿射变换—旋转功能
Aug 29 Python
Python爬取豆瓣视频信息代码实例
Nov 16 Python
python3中利用filter函数输出小于某个数的所有回文数实例
Nov 24 Python
Keras 在fit_generator训练方式中加入图像random_crop操作
Jul 03 Python
Python join()函数原理及使用方法
Nov 14 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
新版mysql+apache+php Linux安装指南
2006/10/09 PHP
PhpMyAdmin出现export.php Missing parameter: what /export_type错误解决方法
2012/08/09 PHP
php实现在限定区域里自动调整字体大小的类实例
2015/04/02 PHP
PHP类相关知识点实例总结
2016/09/28 PHP
php简单中奖算法(实例)
2017/08/15 PHP
kindeditor 加入七牛云上传的实例讲解
2017/11/12 PHP
使用tp框架和SQL语句查询数据表中的某字段包含某值
2019/10/18 PHP
Javascript操纵Cookie实现购物车程序
2006/11/23 Javascript
jquery 操作DOM案例代码分享
2012/04/05 Javascript
jQuery动态添加、删除元素的方法
2014/01/09 Javascript
jquery.ajax的url中传递中文乱码问题的解决方法
2014/02/07 Javascript
JS应用正则表达式转换大小写示例
2014/09/18 Javascript
跟我学习javascript的隐式强制转换
2015/11/16 Javascript
jQuery-1.9.1源码分析系列(十一)DOM操作续之克隆节点
2015/12/01 Javascript
Angular实现一个简单的多选复选框的弹出框指令实例
2017/04/25 Javascript
JavaScript队列函数和异步执行详解
2017/06/19 Javascript
原生JS实现隐藏显示图片 JS实现点击切换图片效果
2021/01/27 Javascript
vue2.0 datepicker使用方法
2018/02/04 Javascript
Vue中key的作用示例代码详解
2020/06/10 Javascript
Python中使用urllib2防止302跳转的代码例子
2014/07/07 Python
Python使用reportlab将目录下所有的文本文件打印成pdf的方法
2015/05/20 Python
Python中XlsxWriter模块简介与用法分析
2018/04/24 Python
Python中的Socket 与 ScoketServer 通信及遇到问题解决方法
2019/04/01 Python
python查询文件夹下excel的sheet名代码实例
2019/04/02 Python
HTML5触摸事件实现移动端简易进度条的实现方法
2018/05/04 HTML / CSS
html5利用canvas实现颜色容差抠图功能
2019/12/23 HTML / CSS
俄罗斯小米家用电器、电子产品和智能家居商店:Poood.ru
2020/04/03 全球购物
上课睡觉检讨书
2014/01/28 职场文书
公司会计岗位职责
2014/02/13 职场文书
环保建议书100字
2014/05/14 职场文书
捐款倡议书格式范文
2014/05/14 职场文书
2014入党积极分子批评与自我批评思想汇报
2014/09/20 职场文书
个人自荐书范文
2015/03/09 职场文书
运动会致辞稿
2015/07/29 职场文书
大学生如何逃脱“毕业季创业队即散伙”魔咒?
2019/08/19 职场文书
Python爬虫之自动爬取某车之家各车销售数据
2021/06/02 Python