0

I am trying to find the index of a number that is greater than 10 but the next value is less than 10:

py =    np.array([    9.7,   10.1,   10.5,  10.2,  10.1,  9.9,   9.8])

So the answer should be 4. However, the output I got was 0. How do I fix the error?

for k in range(0,len(py)):
    if py[k]>10 and py[k+1]<10:
        position_y = np.argmax(k)
        print(position_y)
3
  • Your first number is 9.7 which is <10. So its False, for the next 4 numbers, it is greater than 10 but their successors are not, except the last one. Only `1 is the answer Commented Jul 25, 2021 at 11:00
  • 2
    You probably want to take a look at the docs for np.argmax. It's not clear what you expect it to do here. Really you can just print(k) Commented Jul 25, 2021 at 11:00
  • The numpy.argmax() function returns indices of the max element of the array in a particular axis. Commented Jul 25, 2021 at 11:08

2 Answers 2

2
import numpy as np
py = np.array([    9.7,   10.1,   10.5,  10.2,  10.1,  9.9,   9.8])

for index, value in enumerate(py):
    if value > 10 and py[index + 1] < 10:
        print(index)
Sign up to request clarification or add additional context in comments.

Comments

1
for i in range(len(py)):
if (py[i]>10 and py[i+1]<10):
    print(i)

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.