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统计文件行数示例分享
Feb 21 Python
Python实现国外赌场热门游戏Craps(双骰子)
Mar 31 Python
浅谈对yield的初步理解
May 29 Python
Windows 7下Python Web环境搭建图文教程
Mar 20 Python
Python列表(List)知识点总结
Feb 18 Python
Python3内置模块random随机方法小结
Jul 13 Python
python函数参数(必须参数、可变参数、关键字参数)
Aug 16 Python
python getpass实现密文实例详解
Sep 24 Python
使用python求斐波那契数列中第n个数的值示例代码
Jul 26 Python
Python -m参数原理及使用方法解析
Aug 21 Python
python switch 实现多分支选择功能
Dec 21 Python
利用python Pandas实现批量拆分Excel与合并Excel
May 23 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操作MongoDB时的整数问题及对策说明
2011/05/02 PHP
PHP快速按行读取CSV大文件的封装类分享(也适用于其它超大文本文件)
2014/04/10 PHP
PHP之预定义接口详解
2015/07/29 PHP
浅析PHP关键词替换的类(避免重复替换,保留与还原原始链接)
2015/09/22 PHP
php检查页面是否被百度收录
2015/10/28 PHP
总结PHP删除字符串最后一个字符的三种方法
2016/08/30 PHP
php 伪造HTTP_REFERER页面URL来源的三种方法
2016/09/22 PHP
Centos 6.5系统下编译安装PHP 7.0.13的方法
2016/12/19 PHP
php实现XML和数组的相互转化功能示例
2017/02/08 PHP
PHP面向对象五大原则之开放-封闭原则(OCP)详解
2018/04/04 PHP
基于js disabled="false"不起作用的解决办法
2013/06/26 Javascript
jquery 实现窗口的最大化不论什么情况
2013/09/03 Javascript
JavaScript中为什么null==0为false而null大于=0为true(个人研究)
2013/09/16 Javascript
jQuery前端开发35个小技巧
2016/05/24 Javascript
JS中判断字符串中出现次数最多的字符及出现的次数的简单实例
2016/06/03 Javascript
AngularJS  ng-table插件设置排序
2016/09/21 Javascript
Node.JS使用Sequelize操作MySQL的示例代码
2017/10/09 Javascript
通过jquery的ajax请求本地的json文件方法
2018/08/08 jQuery
vue实现Excel文件的上传与下载功能的两种方式
2019/06/28 Javascript
Vue中的transition封装组件的实现方法
2019/08/13 Javascript
vue + axios get下载文件功能
2019/09/25 Javascript
微信小程序实现拖拽功能
2019/09/26 Javascript
解决vue项目router切换太慢问题
2020/07/19 Javascript
python3操作微信itchat实现发送图片
2018/02/24 Python
Python3的socket使用方法详解
2020/02/18 Python
巴西网上药房:onofre
2016/11/21 全球购物
夏洛特和乔治婴儿和儿童时装精品店:Charlotte and George
2018/06/06 全球购物
上海某公司.net方向笔试题
2014/09/14 面试题
领导干部群众路线教育实践活动个人对照检查材料
2014/09/23 职场文书
工作经验交流材料
2014/12/30 职场文书
党员倡议书
2015/01/19 职场文书
体育教师教学随笔
2015/08/15 职场文书
还在手动盖楼抽奖?教你用Python实现自动评论盖楼抽奖(一)
2021/06/07 Python
浅谈MySQL中的六种日志
2022/03/23 MySQL
搭建zabbix监控以及邮件报警的超级详细教学
2022/07/15 Servers