【python cookbook】【数据结构与算法】12.找出序列中出现次数最多的元素
2023-09-14 09:01:12 时间
问题:找出一个元素序列中出现次数最多的元素是什么
解决方案: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对象都是你手边的得力工具。比起利用字典自己手写算法,更应采用该方式完成任务。
相关文章
- 20 本优秀的 Python 电子书
- python案例-判断素数
- Python快速学习02:基本数据类型 & 序列
- Python的win32serviceutil之疑似BUG
- 【python cookbook】【数据结构与算法】10.从序列中移除重复项且保持元素间顺序不变
- 【python cookbook】【数据结构与算法】18.将名称映射到序列的元素中
- 【python cookbook】【数据结构与算法】10.从序列中移除重复项且保持元素间顺序不变
- Python 刷Leetcode题库,顺带学英语单词(31)
- Python Django 参数解包及代码示例
- Python编程:利用python编程实现对基于时间序列的数据(dataframe格式)按照指定时间范围进行单方向关联,不存在的日期补充为默认的NaN
- 【负荷预测】年月日及时间序列负荷预测(Python代码实现)
- 基于 GARCH -LSTM 模型的混合方法进行时间序列预测研究(Python代码实现)
- 几行 Python 代码就可以提取数百个时间序列特征
- 时间序列分析中最值得推荐的10个 Python 库
- 【推荐收藏】时间序列分析全面指南(附Python代码)
- Python map() ==>根据提供的函数对指定序列做映射
- Python实现Prophet时间序列数据建模与异常值检测(Prophet算法)项目实战
- 【华为OD机试 2023】 最大化控制资源成本(C++ Java JavaScript Python)
- Python:Scrapy传入自定义参数运行
- python 元组 序列上使用enumerate()函数
- Python: 在序列上执行聚集函数(比如sum() , min() , max() )
- Python: collections.nametuple()--映射名称到序列元素
- python基础教程_学习笔记1:序列-1
- Python标准库:内置函数str(object='') str(object=b'', encoding='utf-8', errors='strict')
- 【2023最全教程】Python接口测试实战之接口测试工具的使用(建议收藏)
- shell脚本中执行python脚本并接收其返回值的例子
- 【Leetcode刷题Python】108. 将有序数组转换为二叉搜索树
- 【Leetcode刷题Python】105. 从前序与中序遍历序列构造二叉树
- Python enumerate() 函数【将可遍历的数据对象(如列表、元组或字符串)变为一个索引序列】
- 第7讲:Python中整数序列Range函数的概念以及使用
- Python自动化之使用loguru优雅输出日志