python 实现返回一个列表中出现次数最多的元素方法
作者:毛宝宝 时间:2023-01-04 14:36:54
如下所示:
# 返回一个列表中出现次数最多的元素
def showmax(lt):
index1 = 0 #记录出现次数最多的元素下标
max = 0 #记录最大的元素出现次数
for i in range(len(lt)):
flag = 0 #记录每一个元素出现的次数
for j in range(i+1,len(lt)): #遍历i之后的元素下标
if lt[j] == lt[i]:
flag += 1 #每当发现与自己相同的元素,flag+1
if flag > max: #如果此时元素出现的次数大于最大值,记录此时元素的下标
max = flag
index1 = i
return lt[index1] #返回出现最多的元素
lt = [1,1,2,3,3,5,6,8,9,4,6,18,6,44,6,44,44,44]
print(showmax(lt))
来源:https://blog.csdn.net/chen1042246612/article/details/81191291
标签:python,列表,元素
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python 将字符串中的数字相加求和的实现
2022-11-03 22:31:08
![](https://img.aspxhome.com/file/2023/4/66004_0s.png)
PHP getNamespaces()函数讲解
2023-06-11 07:32:45
![](https://img.aspxhome.com/file/2023/7/55407_0s.png)
python如何通过psutil获取服务器cpu、内存、磁盘使用率
2022-11-07 01:01:29
php 常用算法和时间复杂度
2023-11-05 10:30:49
深度学习环境搭建anaconda+pycharm+pytorch的方法步骤
2023-06-27 02:02:49
![](https://img.aspxhome.com/file/2023/7/77677_0s.png)
浅谈Python 命令行参数argparse写入图片路径操作
2023-03-18 21:08:11
![](https://img.aspxhome.com/file/2023/3/82643_0s.jpg)
Linux系统(X64)安装Oracle11g完整安装图文教程另附基本操作
2023-07-07 08:25:36
![](https://img.aspxhome.com/file/2023/0/63360_0s.jpg)
asp可设置参数的分页代码例子
2007-10-07 12:34:00
asp 关键词高亮显示(不区分大小写)
2011-04-07 10:55:00
python监控linux内存并写入mongodb(推荐)
2022-02-04 11:28:01
Firefox 的 Jetpack 扩展案例分析:Gmail 邮件提醒
2009-10-15 12:41:00
![](https://img.aspxhome.com/file/UploadPic/200910/15/gmail01-18s.png)
区别div和span、relative和absolute、display和visibility
2009-12-13 12:18:00
Python编写简单的HTML页面合并脚本
2022-08-13 09:13:07
python脚本实现验证码识别
2022-02-03 09:41:30
![](https://img.aspxhome.com/file/2023/4/79664_0s.jpg)
如何编写一个高效的国税系统通讯录数据库?
2009-11-07 18:53:00
黄相如:如何做好用户体验
2008-06-04 17:34:00
Pytorch实现List Tensor转Tensor,reshape拼接等操作
2021-06-06 19:58:51
![](https://img.aspxhome.com/file/2023/6/80786_0s.png)
Python装饰器实现方法及应用场景详解
2022-04-30 22:57:49
python广度优先搜索得到两点间最短路径
2023-09-05 02:06:24
![](https://img.aspxhome.com/file/2023/1/88981_0s.jpg)
Python利用memory_profiler查看内存占用情况
2022-05-24 08:55:29
![](https://img.aspxhome.com/file/2023/0/81810_0s.png)