Python find first instance of non zero number in list

后端 未结 9 1359
旧巷少年郎
旧巷少年郎 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:28

    You can use numpy.nonzero: http://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.nonzero.html

    myList = [0.0 , 0.0, 0.0, 2.0, 2.0]
    I = np.nonzero(myList)
    #the first index is necessary because the vector is within a tuple
    first_non_zero_index = I[0][0]
    #3
    

提交回复
热议问题