黑松山资源网 Design By www.paidiu.com
本文实例讲述了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函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月08日
2024年10月08日
- 金铲铲之战S9阵容推荐攻略 6套阵容分享
- dnf手游追忆光环外观选哪个 dnf手游追忆光环外观选择推荐
- 怎么获得dnf手游紫色曲玉 dnf手游紫色曲玉获取方法一览
- dnf手游洗点水怎么获得 dnf手游洗点水获取方法介绍
- 万代南梦宫遭中国玩家抵制 《铁拳8》被疯狂刷差评
- 曝侃爷分手登微博热搜 网友:穿衣意见不合?
- 《暗喻幻想:ReFantazio》M站2024高分榜排名第三 超越《最终幻想7:重生》
- 赛季末发育路上分英雄推荐,狄仁杰A就行
- 小乔克制攻略:除了兰陵王还有更多好选
- 王者荣耀:对抗路难度排行,花木兰最能秀
- 群星《新说唱2024 第3期 (下)》[FLAC/分轨][89.04MB]
- 群星《歌手2024 第2期》[320K/MP3][95.59MB]
- 群星《歌手2024 第2期》[FLAC/分轨][260.73MB]
- 《原神》5.0基尼奇一图流养成攻略 基尼奇怎么培养
- 5.1版本up池角色一览,新版本角色卡池