Python中的min及返回最小值索引的操作
1、Python的min函數(shù)返回列表中的最小的項。
2、如何返回列表中最小的項的索引?
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ù)為序列中的一個元素。
list1.index(max(list1))
結(jié)果返回參數(shù)在列表中的索引
list1 = [123, 456, 789]print(list1.index(456)) #1print(list1.index(max(list1))) #2
最小值只需要將max換成min即可
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章:
1. ASP中格式化時間短日期補(bǔ)0變兩位長日期的方法2. 詳解CSS偽元素的妙用單標(biāo)簽之美3. XML入門的常見問題(四)4. 詳解JS前端使用迭代器和生成器原理及示例5. HTML DOM setInterval和clearInterval方法案例詳解6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. 使用css實現(xiàn)全兼容tooltip提示框8. asp中response.write("中文")或者js中文亂碼問題9. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)10. php bugs代碼審計基礎(chǔ)詳解
