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实现根据指定端口探测服务器/模块部署的方法
Aug 25 Python
寻找网站后台地址的python脚本
Sep 01 Python
Python实现调度算法代码详解
Dec 01 Python
Python3实现发送QQ邮件功能(文本)
Dec 15 Python
利用python将图片转换成excel文档格式
Dec 30 Python
Python字典操作详细介绍及字典内建方法分享
Jan 04 Python
Python3爬虫学习之应对网站反爬虫机制的方法分析
Dec 12 Python
对sklearn的使用之数据集的拆分与训练详解(python3.6)
Dec 14 Python
python+pyqt5实现KFC点餐收银系统
Jan 24 Python
Django 重写用户模型的实现
Jul 29 Python
Python 存取npy格式数据实例
Jul 01 Python
详解Selenium-webdriver绕开反爬虫机制的4种方法
Oct 28 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网上调查系统
2006/10/09 PHP
php列出一个目录下的所有文件的代码
2012/10/09 PHP
使用CodeIgniter的类库做图片上传
2014/06/12 PHP
ThinkPHP打水印及设置水印位置的方法
2016/10/14 PHP
php分页查询的简单实现代码
2017/03/14 PHP
js 巧妙去除数组中的重复项
2010/01/25 Javascript
javascript 判断中文字符长度的函数代码
2012/08/27 Javascript
javascipt基础内容--需要注意的细节
2013/04/10 Javascript
JS复制到剪贴板示例代码
2013/10/30 Javascript
addEventListener()第三个参数useCapture (Boolean)详细解析
2013/11/07 Javascript
js的[defer]和[async]属性
2014/11/24 Javascript
在AngularJS中使用AJAX的方法
2015/06/17 Javascript
浅谈jQuery.easyui的datebox格式化时间
2015/06/25 Javascript
JS实现的竖向折叠菜单代码
2015/10/21 Javascript
详解Angular路由 ng-route和ui-router的区别
2017/05/22 Javascript
关于JavaScript中的this指向问题总结篇
2017/07/23 Javascript
JavaScript中的一些隐式转换和总结(推荐)
2017/12/22 Javascript
浅谈Vue.use的使用
2018/08/29 Javascript
Python使用minidom读写xml的方法
2015/06/03 Python
python中管道用法入门实例
2015/06/04 Python
Python使用正则表达式抓取网页图片的方法示例
2017/04/21 Python
Python基于回溯法子集树模板解决选排问题示例
2017/09/07 Python
Python get获取页面cookie代码实例
2018/09/12 Python
Python time库基本使用方法分析
2019/12/13 Python
MNIST数据集转化为二维图片的实现示例
2020/01/10 Python
如何基于Python爬虫爬取美团酒店信息
2020/11/03 Python
python爬虫使用scrapy注意事项
2020/11/23 Python
Marriott中国:万豪国际酒店查询预订
2016/09/02 全球购物
函授毕业生自我鉴定范文
2014/03/25 职场文书
2014年接待工作总结
2014/11/26 职场文书
2014年高数考试作弊检讨书
2014/12/14 职场文书
2015年发展党员工作总结报告
2015/03/31 职场文书
详解解Django 多对多表关系的三种创建方式
2021/08/23 Python
Vue的过滤器你真了解吗
2022/02/24 Vue.js
Python Pygame实战在打砖块游戏的实现
2022/03/17 Python
Python读写yaml文件
2022/03/20 Python