l = [1,2,3]
maxnum = max(l)
print(l.index(maxnum))
index函數只會返回列表裏第壹個匹配的值,如果最大值在列表裏有多個,則無法全部查詢到
壹個冒泡排序的思路,逐壹對比,並記住當前最大值的下標,可以得到最大值的多個下標
l = [1,2,3,0,3]
indeiesDict = {}
maxnum = 0
for i in range(len(l)):
if i > 0 :
if l[i] >= l[i-1]:
maxnum = l[i]
index = i
else:
maxnum = l[i-1]
index = i-1
if indeiesDict.get(maxnum):
indeiesDict[maxnum].add(index)
else:
indeiesDict[maxnum] = set([index])
print(maxnum,indeiesDict[maxnum])