Python面试题之统计哈希列表中最多元素
作者:沉沉沉小姐 时间:2023-06-26 20:24:49
问题
有一个元素序列,想知道在序列中出现次数最多的元素是什么
解决方案
collections 模块中的 Counter 类转让给女士为此问题所设计的。它甚至有一个非常方便的most_common()方法可以直接告诉我们答案。
为了说明用法,假设有一个列表,列表中是一系列的单词,我们想找出哪些单词出现的最为频繁。
下面是我们的做法:
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)]
讨论可以给 Counter 对象提供任何可哈希的对象序列做为输入。在底层实现中,Counter 是一个字典,在元素和它们出现的次数间做了映射。例:
word_counter['not']
# 1
word_counter['eyes']
# 8
如果想手动增加计数,只能简单地自增即可:
morewords = ['why','are','you','not','looking','in','my','eyes']
for word in morewords:
word_counts[word] += 1
print(word_counts['eyes'])
# 9
另一种方法是使用update()方法:
word_counts.update(morewords)
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://blog.csdn.net/chenyuhuaxin/article/details/100566584
标签:Python,哈希列表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决python gdal投影坐标系转换的问题
2021-11-02 12:14:55
SQL Server误区30日谈 第29天 有关堆碎片的误区
2024-01-20 06:20:06
Python装饰器用法实例分析
2023-02-22 12:05:49
Python设计模式结构型代理模式
2023-06-30 19:23:58
asp 得到动态数组中元素的个数
2011-03-30 10:55:00
python转化excel数字日期为标准日期操作
2021-01-14 22:38:59
![](https://img.aspxhome.com/file/2023/7/107877_0s.jpg)
Python 自动化表单提交实例代码
2022-12-20 06:16:14
![](https://img.aspxhome.com/file/2023/5/77825_0s.png)
Python MySQL数据库连接池组件pymysqlpool详解
2024-01-22 23:59:17
![](https://img.aspxhome.com/file/2023/7/115047_0s.png)
MSSQL 事务说明
2024-01-25 12:18:23
SQL Server中T-SQL标识符介绍与无排序生成序号的方法
2024-01-17 11:54:45
在Pycharm中设置默认自动换行的方法
2022-09-10 04:29:00
FrontPage2002简明教程四:网页超级链接
2008-09-17 11:23:00
![](https://img.aspxhome.com/file/UploadPic/200810/17/20081017113025456s.jpg)
ASP UTF-8编码下字符串截取和获取长度函数
2011-03-30 10:52:00
详解Python中的变量及其命名和打印
2023-07-23 11:31:45
Python实现命令行通讯录实例教程
2023-10-18 01:51:28
![](https://img.aspxhome.com/file/2023/2/82882_0s.png)
Go每日一库之dateparse处理时间
2024-04-26 17:25:55
MySql完整卸载的四个步骤详解
2024-01-18 00:30:52
![](https://img.aspxhome.com/file/2023/0/80020_0s.jpg)
GO语言实现简单TCP服务的方法
2024-02-02 23:40:26
MySQL优化教程之超大分页查询
2024-01-28 08:30:57
![](https://img.aspxhome.com/file/2023/1/106591_0s.png)
Http头 Range、Content-Range
2010-06-25 19:19:00