2.2 Most Element A

Description

Input a list and find out the most elements. If the numbers of elements are the same, output the front element in the list.

Input
[3,2,3]

Output
3

code

  • 谷歌翻译:输入一个列表,找出最多的元素。如果元素个数相同,则输出列表中最前面的元素。
  • 代码:
lst = eval(input())  # 如果没有eval的话会自动视为一个字符串
dict1 = {}
i = len(lst)  # len()求出列表的长度
while i:
    i -= 1  # 长度要先-1,不然就越界了
    if lst[i] not in dict1.keys():  # 避免重复添加
        dict1[lst[i]] = lst.count(lst[i])  # count()这个方法可以计算它的参数在列表中出现的次数

most = 0
index = -1
for ky in dict1:
    if dict1[ky] > most:
        most = dict1[ky]
        index = ky

print(index)

# print(lst)
# print(dict1)

# [4,5,3,2,5,7,5]

summary

  1. len() 返回长度
  2. .count() 返回元素出现的数量
  3. eval() 评估函数
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很可能是由于循环导入引起的。循环导入是指两个或多个模块相互引用,导致无法正确加载模块的情况。当一个模块试图导入另一个模块时,如果被导入的模块又试图导入第一个模块,就会出现循环导入的问题。这种情况下,Python解释器无法确定模块的加载顺序,从而导致ImportError。解决循环导入问题的方法有多种,可以通过重构代码,将导入语句放在函数内部,或者使用延迟导入等方式来避免循环导入的发生。\[2\]\[3\] #### 引用[.reference_title] - *1* [Python 进行debug的错误most likely due to a circular import](https://blog.csdn.net/domyself918/article/details/123379364)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [python 报错 most likely due to a circular import 解决方法](https://blog.csdn.net/whatday/article/details/109333877)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [【ModuleNotFoundError 与 ImportError】之 most likely due to a circular import](https://blog.csdn.net/fancunshuai/article/details/125103676)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值