最新下载
热门教程
- 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即可
相关文章
- 三国志8重制版虚构特典剧本介绍说明 10-30
- 暗喻幻想暗黑法师解锁方法攻略分享 10-30
- 暗喻幻想元素大师解锁方法攻略分享 10-30
- 暗喻幻想地下纳骨堂锁住的门打开方法 10-30
- 暗喻幻想6月22日玛丽亚位置一览 10-30
- 暗喻幻想巫师阿基态解锁方法分享 10-30