python 找出list中最大或者最小几个数的索引方法
作者:ShawDa 时间:2022-08-12 13:23:11
如下所示:
nums = [1,8,2,23,7,-4,18,23,24,37,2]
result = map(nums.index, heapq.nlargest(3, nums))
temp=[]
Inf = 0
for i in range(3):
temp.append(nums.index(max(nums)))
nums[nums.index(max(nums))]=Inf
result.sort()
temp.sort()
print(result)
print(temp)
如上,有result和temp两种求法,上面代码输出:
[3, 8, 9]
[3, 8, 9]
没问题
但是把nums改一下:
nums = [1,8,2,23,7,-4,18,23,23,37,2]
输出:
[3, 3, 9]
[3, 7, 9]
发现问题了吧,result方法发现相同数字永远会返回第一次出现的索引。
来源:https://blog.csdn.net/sinat_36811967/article/details/79572960
标签:python,list,索引
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python+Yolov5人脸口罩识别的详细步骤
2022-05-19 23:35:37
![](https://img.aspxhome.com/file/2023/9/128269_0s.jpg)
asp.net cookie清除的代码
2023-07-19 15:24:37
laravel中短信发送验证码的实现方法
2023-11-15 08:05:16
![](https://img.aspxhome.com/file/2023/0/91210_0s.jpg)
python super()函数的详解
2023-08-08 10:09:42
![](https://img.aspxhome.com/file/2023/5/107265_0s.png)
python不相等的两个字符串的 if 条件判断为True详解
2021-12-12 16:54:51
![](https://img.aspxhome.com/file/2023/8/128638_0s.jpg)
pandas中按行或列的值对数据排序的实现
2023-09-23 02:32:18
Python爬虫之教你利用Scrapy爬取图片
2022-11-02 10:35:02
![](https://img.aspxhome.com/file/2023/6/80736_0s.png)
使用Python编写一个最基础的代码解释器的要点解析
2023-07-16 11:47:22
![](https://img.aspxhome.com/file/2023/4/107974_0s.jpg)
ThinkPHP函数详解之M方法和R方法
2023-11-22 12:00:25
python文件路径操作方法总结
2023-04-30 21:00:15
解决使用OpenCV中的imread()内存报错问题
2022-06-06 03:14:21
![](https://img.aspxhome.com/file/2023/8/95598_0s.jpg)
SQL Server数据库内存会不断增加的问题分析
2009-01-08 15:46:00
十万条Access数据表分页的两个解决方法
2008-05-23 18:24:00
Python+OpenCV图像处理——实现轮廓发现
2023-12-12 23:30:31
![](https://img.aspxhome.com/file/2023/5/97505_0s.png)
python爬虫教程之bs4解析和xpath解析详解
2023-09-22 19:43:06
Pycharm新建模板默认添加个人信息的实例
2022-03-30 20:59:50
![](https://img.aspxhome.com/file/2023/5/80585_0s.jpg)
Python如何创建装饰器时保留函数元信息
2023-03-29 12:32:46
几种修复ACCESS数据库的实用方法
2008-11-20 17:37:00
Python爬虫:url中带字典列表参数的编码转换方法
2021-11-02 17:50:45
Python爬虫之Selenium多窗口切换的实现
2021-09-25 17:48:48