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中的filter和lambda函数的使用
Apr 07 Python
Python中转换角度为弧度的radians()方法
May 18 Python
简单介绍Python中的readline()方法的使用
May 24 Python
Window 64位下python3.6.2环境搭建图文教程
Sep 19 Python
Python 等分切分数据及规则命名的实例代码
Aug 16 Python
opencv实现简单人脸识别
Feb 19 Python
决策树剪枝算法的python实现方法详解
Sep 18 Python
pytorch:torch.mm()和torch.matmul()的使用
Dec 27 Python
python中如何使用insert函数
Jan 09 Python
Django实现任意文件上传(最简单的方法)
Jun 03 Python
django数据模型中null和blank的区别说明
Sep 02 Python
Python爬虫基础初探selenium
May 31 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/07/17 PHP
ThinkPHP在Cli模式下使用模板引擎的方法
2015/09/25 PHP
PHP动态生成指定大小随机图片的方法
2016/03/25 PHP
php版交通银行网银支付接口开发入门教程
2016/09/26 PHP
php实现将二维关联数组转换成字符串的方法详解
2017/07/31 PHP
针对thinkPHP5框架存储过程bug重写的存储过程扩展类完整实例
2018/06/16 PHP
asp.net 30分钟掌握无刷新 Repeater
2011/09/16 Javascript
jquery属性过滤选择器使用示例
2013/06/18 Javascript
JavaScript的21条基本知识点
2014/03/04 Javascript
Js控制滑轮左右滑动实例
2015/02/13 Javascript
原生JavaScript实现Ajax的方法
2016/04/07 Javascript
JavaScript中的跨浏览器事件操作的基本方法整理
2016/05/20 Javascript
基于原生js淡入淡出函数封装(兼容IE)
2016/10/20 Javascript
微信小程序开发实战教程之手势解锁
2016/11/18 Javascript
Python3中的2to3转换工具使用示例
2015/06/12 Python
python开发之thread线程基础实例入门
2015/11/11 Python
Python升级导致yum、pip报错的解决方法
2017/09/06 Python
详解使用 pyenv 管理多个版本 python 环境
2017/10/19 Python
Request的中断和ErrorHandler实例解析
2018/02/12 Python
python实现装饰器、描述符
2018/02/28 Python
对Python中的@classmethod用法详解
2018/04/21 Python
对python xlrd读取datetime类型数据的方法详解
2018/12/26 Python
Python DataFrame一列拆成多列以及一行拆成多行
2019/08/06 Python
Python爬取智联招聘数据分析师岗位相关信息的方法
2019/08/13 Python
加拿大最大的箱包及旅游配件零售商:Bentley Leathers
2017/07/19 全球购物
美国著名的户外用品品牌:L.L.Bean
2018/01/05 全球购物
北美个性化礼品商店:Things Remembered
2018/06/12 全球购物
英国领先的在线礼品店:Getting Personal
2019/09/24 全球购物
Haggar官网:美国男装品牌
2020/02/16 全球购物
电子信息科学专业自荐信
2014/01/30 职场文书
调解员先进事迹材料
2014/02/07 职场文书
《奇妙的国际互联网》 教学反思
2014/02/25 职场文书
小学优秀辅导员事迹材料
2014/05/11 职场文书
义务教育学校标准化建设汇报材料
2014/08/16 职场文书
运动会开幕式新闻稿
2015/07/17 职场文书
python基础详解之if循环语句
2021/04/24 Python