Reputation: 99
Thanks for checking this out for me. New to python. So, I have an array, time=[1,2,3,4,5,6,7,8....] and I need the index of the first element where time > 7. What I have so far: time.index( np.where( time > 7)) getting error: AttributeError: 'numpy.ndarray' object has no attribute 'index' This was s hot in the dark so far. help please! Thanks!
Upvotes: 0
Views: 2803
Reputation: 238131
If you use numpy, you can do as follows:
time_l=[1,2,3,4,5,6,7,8]
import numpy as np
a = np.array(time_l)
print(np.where(a > 7))
# Prints (array([7]),)
Dont need to use index on your list with numpy.
You can also use list comprehension:
print([i for i,v in enumerate(time_l) if v > 7])
# gives: [7]
Alternative way, with generator:
time_l=[1,2,3,4,5,6,7,8,9,10]
print(next(i for i,v in enumerate(time_l) if v > 7))
# prints 7
And more intuitive way, using for loop and index:
for v in time_l:
if v > 7:
print(time_l.index(v))
break
Upvotes: 1