0

I am trying to compare values in a numpy array with a scalar value. Here is an example of this array. If it's any help it can only contain positive values.

y = np.array([ 1 , 0.008 , 3 , 4 , 5])

In another section of my code I want to compare this array with a scalar, say 3.5 in an 'if' statement used to break while loop. I want to test if ANY of the values in the array is larger then this scalar value.

while True:

    if any_value_in_array(y) > 3.5
       break

    #random code

Any command that would enable me of doing something like that?

1 Answer 1

1

You could use any() on the condition check

In [377]: (y > 3.5).any()
Out[377]: True

Longer Example

In [378]: y
Out[378]: array([ 1.   ,  0.008,  3.   ,  4.   ,  5.   ])

In [379]: (y > 3.5)
Out[379]: array([False, False, False,  True,  True], dtype=bool)

In [380]: (y > 3.5).any()
Out[380]: True

Additionally, if you want to check if all elements match the condition, you could use all()

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.