Home > OS >  Get items between two values in an np.array python
Get items between two values in an np.array python

Time:11-16

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!

CodePudding user response:

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])

  • Related