用Python查找数组中的最小值,并返回它的索引(下标)

#coding=utf-8
‘’’
查找数组中的最小值,并返回它的索引
Created on 2019年12月8日
@author: LWJ
‘’’
def findSmallest(arr):
smallest = arr[0] #存储最小的值
smallest_index = 0 #存储最小元素的索引
for i in range(1,len(arr)):
if arr[i] < smallest:
smallest = arr[i]
smallest_index = i
return smallest_index

print findSmallest([10,11,4,7,9,44,2,55])

用Python查找数组中的最小值,并返回它的索引(下标)_第1张图片

用Python查找数组中的最小值,并返回它的索引(下标)_第2张图片

你可能感兴趣的:(Python)