最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python中的min及返回最小值索引操作代码
时间:2022-06-25 02:02:45 编辑:袖梨 来源:一聚教程网
本篇文章小编给大家分享一下Python中的min及返回最小值索引操作代码,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。
1、Python的min函数返回列表中的最小的项。
2、如何返回列表中最小的项的索引?
def indexofMin(arr): minindex = 0 currentindex = 1 while currentindex < len(arr): if arr[currentindex] < arr[minindex]: minindex = currentindex currentindex += 1 return minindex arr = [3,5,2,1] print(indexofMin(arr))
补充:python返回列表中的最大值(最小值)与其索引
1. 返回列表最大值
使用方法:max()
其语法:该函数返回给定参数的最大值,参数可以为序列。
n = max(list) #list 表示要返回最大值的列表。
结果:返回列表元素中的最大值
list1 = [123, 456, 789] list2 = ['123', '456', '789'] list3 = ['abc', 'abb', 'acb'] print(max(list1)) #789 print(max(list2)) #789 print(max(list3)) #acb
2. 返回列表最大值的索引
使用方法:利用max找到列表中的最大值,
利用再index()找到最大值的索引
该函数返回给定参数索引,参数为序列中的一个元素。
list1.index(max(list1))
结果返回参数在列表中的索引
list1 = [123, 456, 789] print(list1.index(456)) #1 print(list1.index(max(list1))) #2
最小值只需要将max换成min即可
相关文章
- Jsp servlet验证码工具类分享 07-03
- Dreamweaver怎么设置经典风格视图? 07-03
- 资讯:如何精准识别高回报的加密货币空投项目? 07-03
- 银与绯眷属强度排行一览 07-03
- mysql存储过程游标之loop循环解读 07-03
- PS照片处理图片3d立体拼贴技巧详解 五步骤快速制作 07-03