1

I'm new to Phython and was wondering if anyone can help me with this exercise: I have an array in python and I want to get the elements that have values between 5 and 10.

a = np.array([2, 6, 1, 9, 10, 3, 27])

The exercise asks me to do this in three different ways and gives me the "hint" to use the & operator and np.logical_and() function.

Can anyone please help me out? Thanks!

1
  • better example would have been a = np.array([2, 6, 1, 9, 10, 3, 27, 8]) Commented Nov 14, 2021 at 15:55

1 Answer 1

2

Assuming ≥5 and <10, you can use slicing:

a[(a>=5) & (a<10)]

or

a[np.logical_and(a>=5, a<10)]

output: array([6, 9])

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.