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
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
Here's a one liner to do it:
val = next((index for index,value in enumerate(myList) if value != 0), None)
Basically, it uses next() to find the first value, or return None
if there isn't one. enumerate() is used to make an iterator that iterates over index,value tuples so that we know the index that we're at.
How about doing following:
print (np.nonzero(np.array(myList))[0][0])
This is more convenient because along with finding non-zero values, this can also help to apply logic function directly. For example:
print (np.nonzero(np.array(myList)>1))