Python find first instance of non zero number in list

后端 未结 9 1326
旧巷少年郎
旧巷少年郎 2020-12-30 19:59

I have a list like this

myList = [0.0 , 0.0, 0.0, 2.0, 2.0]

I would like to find the location of the first number in the list that is not e

9条回答
  •  既然无缘
    2020-12-30 20:17

    Using next with enumerate is excellent when the array is large. For smaller arrays, I would use argmax from numpy so that you won't need a loop:

    import numpy as np
    
    myList = [0.0, 0.0, 0.0, 2.0, 2.0]
    myArray = np.array(myList)
    np.argmax(myArray > 0)
    3
    

提交回复
热议问题