gpt4 book ai didi

Python面试题之统计哈希列表中最多元素

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 29 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Python面试题之统计哈希列表中最多元素由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

问题

有一个元素序列,想知道在序列中出现次数最多的元素是什么 。

解决方案

collections 模块中的 Counter 类转让给女士为此问题所设计的。它甚至有一个非常方便的most_common()方法可以直接告诉我们答案.

为了说明用法,假设有一个列表,列表中是一系列的单词,我们想找出哪些单词出现的最为频繁.

下面是我们的做法:

?
1
2
3
4
5
6
7
8
9
10
11
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 是一个字典,在元素和它们出现的次数间做了映射。例:

?
1
2
3
4
word_counter[ 'not' ]
# 1
word_counter[ 'eyes' ]
# 8

如果想手动增加计数,只能简单地自增即可:

?
1
2
3
4
5
morewords = [ 'why' , 'are' , 'you' , 'not' , 'looking' , 'in' , 'my' , 'eyes' ]
for word in morewords:
     word_counts[word] + = 1
print (word_counts[ 'eyes' ])
# 9

另一种方法是使用update()方法:

?
1
word_counts.update(morewords)

Counter对象还可以同各种数学运算操作结合起来使用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>>> 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 })

以上就是Python面试题之统计哈希列表中最多元素的详细内容,更多关于Python哈希列表最多元素统计的资料请关注我其它相关文章! 。

原文链接:https://blog.csdn.net/chenyuhuaxin/article/details/100566584 。

最后此篇关于Python面试题之统计哈希列表中最多元素的文章就讲到这里了,如果你想了解更多关于Python面试题之统计哈希列表中最多元素的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com