0

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!

1
  • Please show some code. This will get you a stronger and quicker answer. Commented Dec 8, 2014 at 1:32

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.