python出现的次数最多的元素_【Python 秘籍】序列中出现次数最多的元素

问题

怎样找出一个序列中出现次数最多的元素呢?

解决方案

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)

# 出现频率最高的3个单词

top_three = word_counts.most_common(3)

print(top_three)

# Outputs [('eyes', 8), ('the', 5), ('look', 4)]

讨论

作为输入, Counter 对象可以接受任意的由可哈希(hashable)元素构成的序列对象。 在底层实现上,一个 Counter 对象就是一个字典,将元素映射到它出现的次数上。比如:

>>> word_counts['not']

1

>>> word_counts['eyes']

8

>>>

如果你想手动增加计数,可以简单的用加法:

>>> morewords = ['why','are','you','not','looking','in','my','eyes']

>>> for word in morewords:

... word_counts[word] += 1

...

>>> 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})

>>>

毫无疑问, Counter 对象在几乎所有需要制表或者计数数据的场合是非常有用的工具。 在解决这类问题的时候你应该优先选择它,而不是手动的利用字典去实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 可以使用Python的Counter模块来实现统计整数序列出现次数最多的数。 具体实现步骤如下: 1. 导入Counter模块 ```python from collections import Counter ``` 2. 定义整数序列 ```python nums = [1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1] ``` 3. 使用Counter模块统计整数序列每个数出现次数 ```python count = Counter(nums) ``` 4. 找出出现次数最多的数 ```python most_common_num = count.most_common(1)[][] ``` 完整代码如下: ```python from collections import Counter nums = [1, 2, 3, 4, 5, 1, 2, 3, 1, 2, 1] count = Counter(nums) most_common_num = count.most_common(1)[][] print("出现次数最多的数是:", most_common_num) ``` 输出结果为: ``` 出现次数最多的数是: 1 ``` ### 回答2: 对于求一个整数序列出现次数最多的数,可以使用Python的Counter()函数来进行统计。Counter()函数是一个计数器对象,它可以帮助我们快速统计一个列表每个元素出现次数,并以字典的形式返回统计结果。 我们可以将整数序列作为Counter()函数的输入参数,然后使用most_common()方法获取出现次数最多元素,它返回一个元素和计数器值的元组。最后,我们可以返回元素的值。 示例代码如下: ```python from collections import Counter def most_frequent(arr): # 使用Counter函数统计序列每个元素出现次数 count = Counter(arr) # 返回出现次数最多元素 return count.most_common(1)[0][0] # 测试 arr = [1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5] print(most_frequent(arr)) # 输出 5 ``` 在这个示例,我们定义了一个名为most_frequent()的函数,它接受一个整数序列作为输入参数。我们首先使用Counter()函数统计序列每个元素出现次数,并将结果存储在变量count。然后,我们使用most_common()方法从计数器获取出现次数最多元素的列表,由于我们只需要获取列表的第一个元素,所以使用[0][0]获取元素的值。 最后,我们使用示例数组进行函数测试,并将输出结果打印到控制台,输出为5,也就是整数序列出现次数最多的数。 ### 回答3: 要求在整数序列找到出现次数最多的数,我们可以采用Python字典来存储每个数字出现次数。具体的实现步骤如下: 1. 输入整数序列 首先,我们需要让用户输入一个整数序列,可以使用input()函数获取用户输入的内容,并将字符串转换成整数列表。具体代码如下: ``` num_list = list(map(int, input().split())) ``` 2. 统计数字出现次数 我们可以使用一个字典来统计每个数字的出现次数,其字典的键是数字,值是该数字在序列出现次数。我们可以遍历整个序列,对于每个数字,如果字典已经有该数字的键,则将该键对应的值加1;如果字典还没有该数字的键,则将该数字作为新的键,并将值设为1。具体代码如下: ``` dict = {} for num in num_list: if num in dict: dict[num] += 1 else: dict[num] = 1 ``` 3. 找到出现次数最多的数字 我们可以遍历字典的值,找到出现次数最多的数字对应的键。具体代码如下: ``` max_num = max(dict, key=dict.get) ``` 4. 输出结果 最后,我们可以输出出现次数最多的数字以及它出现次数,具体代码如下: ``` print("出现次数最多的数字是:", max_num) print("它出现次数是:", dict[max_num]) ``` 完整代码如下所示: ``` num_list = list(map(int, input().split())) dict = {} for num in num_list: if num in dict: dict[num] += 1 else: dict[num] = 1 max_num = max(dict, key=dict.get) print("出现次数最多的数字是:", max_num) print("它出现次数是:", dict[max_num]) ``` 以上就是使用Python求整数序列出现次数最多的数字的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值