python

超轻量级php框架startmvc

Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例

更新时间:2020-05-24 21:12:01 作者:startmvc
本文实例讲述了Python找出序列中出现次数最多的元素。分享给大家供大家参考,具体如下:

本文实例讲述了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 cookbook 数据结构与算法 找出 序列 出现次数最多 元素 算法
相关文章

Pandas-Cookbook 时间戳处理方式

Python cookbook(字符串与文本)在字符串的开头或结尾处进行文本匹配操作

Python cookbook(数据结构与算法)将多个映射合并为单个映射的方法

Python cookbook(字符串与文本)针对任意多的分隔符拆分字符串操作示例

Python cookbook(数据结构与算法)从字典中提取子集的方法示例

Python cookbook(数据结构与算法)将名称映射到序列元素中的方法

Python cookbook(数据结构与算法)同时对数据做转换和换算处理操作示例

Python cookbook(数据结构与算法)找出序列中出现次数最多的元素算法示例

Python cookbook(数据结构与算法)通过公共键对字典列表排序算法示例

Python cookbook(数据结构与算法)实现对不原生支持比较操作的对象排序算法示例

Python cookbook(数据结构与算法)根据字段将记录分组操作示例

Python cookbook(数据结构与算法)筛选及提取序列中元素的方法

Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法

Python cookbook(数据结构与算法)对切片命名清除索引的方法

Python cookbook(数据结构与算法)将序列分解为单独变量的方法

Python cookbook(数据结构与算法)从任意长度的可迭代对象中分解元素操作示例

Python cookbook(数据结构与算法)保存最后N个元素的方法

Python cookbook(数据结构与算法)找到最大或最小的N个元素实现方法示例

Python cookbook(数据结构与算法)实现优先级队列的方法示例

Python cookbook(数据结构与算法)在字典中将键映射到多个值上的方法