利用Python找出序列中出现最多的元素示例代码

作者:标点符 时间:2023-01-19 09:54:43 

前言

Python包含6种内置的序列:列表、元组、字符串 、Unicode字符串、buffer对象、xrange对象。在序列中的每个元素都有自己的编号。列表与元组的区别在于,列表是可以修改,而组元不可修改。理论上几乎所有情况下元组都可以用列表来代替。有个例外是但元组作为字典的键时,在这种情况下,因为键不可修改,所以就不能使用列表。

我们在一些统计工作或者分析过程中,有事会遇到要统计一个序列中出现最多次的元素,比如一段英文中,查询出现最多的词是什么,及每个词出现的次数。一遍的做法为,将每个此作为key,出现一次,value增加1。

例如:


morewords = ['why','are','you','not','looking','in','my','eyes']
for word in morewords:
word_counts[word] += 1

collections.Counter 类就是专门为这类问题而设计的, 它甚至有一个有用的 most_common() 方法直接给了你答案。

collections模块

collections模块自Python 2.4版本开始被引入,包含了dict、set、list、tuple以外的一些特殊的容器类型,分别是:

  • OrderedDict类:排序字典,是字典的子类。引入自2.7。

  • namedtuple()函数:命名元组,是一个工厂函数。引入自2.6。

  • Counter类:为hashable对象计数,是字典的子类。引入自2.7。

  • deque:双向队列。引入自2.4。

  • defaultdict:使用工厂函数创建字典,使不用考虑缺失的字典键。引入自2.5。

文档参见:http://docs.python.org/2/library/collections.html。

Counter类

Counter类的目的是用来跟踪值出现的次数。它是一个无序的容器类型,以字典的键值对形式存储,其中元素作为key,其计数作为value。计数值可以是任意的Interger(包括0和负数)。Counter类和其他语言的bags或multisets很相似。

为了演示,先假设你有一个单词列表并且想找出哪个单词出现频率最高。你可以这样做:


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)
# 出现频率最高的3个单词
top_three = word_counts.most_common(3)
print(top_three)
# Outputs [('eyes', 8), ('the', 5), ('look', 4)]

另外collections.Counter还有一个比较高级的功能,支持数学算术符的相加相减。


>>> a = Counter(words)
>>> b = Counter(morewords)
>>> a
Counter({'eyes': 8, 'the': 5, 'look': 4, 'into': 3, 'my': 3, 'around': 2,
"you're": 1, "don't": 1, 'under': 1, 'not': 1})
>>> b
Counter({'eyes': 1, 'looking': 1, 'are': 1, 'in': 1, 'not': 1, 'you': 1,
'my': 1, 'why': 1})
>>> # Combine counts
>>> c = a + b
>>> c
Counter({'eyes': 9, 'the': 5, 'look': 4, 'my': 4, 'into': 3, 'not': 2,
'around': 2, "you're": 1, "don't": 1, 'in': 1, 'why': 1,
'looking': 1, 'are': 1, 'under': 1, 'you': 1})
>>> # Subtract counts
>>> d = a - b
>>> d
Counter({'eyes': 7, 'the': 5, 'look': 4, 'into': 3, 'my': 2, 'around': 2,
"you're": 1, "don't": 1, 'under': 1})
>>>

参考文档:

https://docs.python.org/3/library/collections.html

来源:https://www.biaodianfu.com/python-list-value-count.html

标签:python,序列,元素
0
投稿

猜你喜欢

  • JS模仿手机端九宫格登录功能实现代码

    2024-04-17 10:33:21
  • php获取通过http协议post提交过来xml数据及解析xml

    2023-11-14 15:43:36
  • Vue中的字符串模板的使用

    2024-05-09 10:42:52
  • 巧用Dreamweaver MX共享Execl XP文件

    2009-07-14 21:56:00
  • Python反向传播实现线性回归步骤详细讲解

    2021-06-04 10:20:03
  • Oracle数据库失效对象处理详情

    2023-07-13 16:42:39
  • 如何在pycharm中快捷安装pip命令(如pygame)

    2023-02-26 14:53:25
  • Python利用flask sqlalchemy实现分页效果

    2023-10-05 16:36:21
  • 解决pytorch-gpu 安装失败的记录

    2023-04-04 05:44:44
  • python中可以声明变量类型吗

    2022-02-13 19:44:05
  • Python实现简易五子棋游戏

    2023-02-04 07:02:22
  • MySQL UNION操作符基础知识点

    2024-01-21 10:24:31
  • 基于OpenCV python3实现证件照换背景的方法

    2023-01-30 06:25:54
  • PHP与以太坊交互详解

    2023-10-21 13:59:36
  • 解决Vue不能检测数组或对象变动的问题

    2024-04-27 15:59:40
  • Appium+python自动化之连接模拟器并启动淘宝APP(超详解)

    2021-03-08 01:07:41
  • oracle学习笔记(二)

    2012-01-05 18:59:20
  • MySQL Community Server 8.0.11安装配置方法图文教程

    2024-01-26 06:02:45
  • python验证码识别的示例代码

    2023-08-04 03:20:24
  • Python中的异常处理try/except/finally/raise用法分析

    2023-12-11 21:37:41
  • asp之家 网络编程 m.aspxhome.com