Python: Finding the last index of min element?

前端 未结 6 455
情书的邮戳
情书的邮戳 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:45

    len(myList)-1 - myList[::-1].index(min(list))
    

    This uses the slice notation list[::-1] to return a shallow copy of a reversed list, so that it doesn't change your original list, then searches for the minimum value in that list

    >>>myList = [1,2,3,4,1,2]
    >>>len(myList)-1 - myList[::-1].index(min(list))
    4
    

提交回复
热议问题