Python中的min及返回最小值索引的操作
1、Python的min函數(shù)返回列表中的最小的項(xiàng)。
2、如何返回列表中最小的項(xiàng)的索引?
def indexofMin(arr): minindex = 0 currentindex = 1 while currentindex < len(arr):if arr[currentindex] < arr[minindex]: minindex = currentindexcurrentindex += 1 return minindexarr = [3,5,2,1]print(indexofMin(arr))
補(bǔ)充:python返回列表中的最大值(最小值)與其索引
1. 返回列表最大值使用方法:max()
其語法:該函數(shù)返回給定參數(shù)的最大值,參數(shù)可以為序列。
n = max(list) #list 表示要返回最大值的列表。
結(jié)果:返回列表元素中的最大值
list1 = [123, 456, 789]list2 = [’123’, ’456’, ’789’]list3 = [’abc’, ’abb’, ’acb’]print(max(list1)) #789print(max(list2)) #789print(max(list3)) #acb2. 返回列表最大值的索引
使用方法:利用max找到列表中的最大值,
利用再index()找到最大值的索引
該函數(shù)返回給定參數(shù)索引,參數(shù)為序列中的一個(gè)元素。
list1.index(max(list1))
結(jié)果返回參數(shù)在列表中的索引
list1 = [123, 456, 789]print(list1.index(456)) #1print(list1.index(max(list1))) #2
最小值只需要將max換成min即可
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. IntelliJ IDEA設(shè)置條件斷點(diǎn)的方法步驟2. IntelliJ IDEA導(dǎo)入jar包的方法3. SSM框架JSP使用Layui實(shí)現(xiàn)layer彈出層效果4. 刪除docker里建立容器的操作方法5. IntelliJ IDEA導(dǎo)出項(xiàng)目的方法6. 基于android studio的layout的xml文件的創(chuàng)建方式7. Python產(chǎn)生batch數(shù)據(jù)的操作8. Java導(dǎo)出Execl疑難點(diǎn)處理的實(shí)現(xiàn)9. 淺談定義一個(gè)PHP函數(shù)10. IDEA創(chuàng)建SpringBoot的maven項(xiàng)目的方法步驟
