Python: Finding the last index of min element?

前端 未结 6 452
情书的邮戳
情书的邮戳 2021-01-12 05:20

For example [1,2,3,4,1,2]

has min element 1, but it occurs for the last time at index 4.

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-12 05:26

    Not so efficient (I'm a beginner in Python), but works fine as well. The idx will just hold the last index of minimum element. I think that M.Keijzers method is the best.

    array = [1,2,3,4,1,2]
    min_val = min(array) 
    for i in range(len(array)):
        if array[i] == min_val:
            idx = i
    
    print idx
    

提交回复
热议问题