Numpy array, how to select indices satisfying multiple conditions?

Posted by Bob on Stack Overflow See other posts from Stack Overflow or by Bob
Published on 2010-06-12T23:28:05Z Indexed on 2010/06/12 23:32 UTC
Read the original article Hit count: 237

Filed under:
|

Suppose I have a numpy array x = [5, 2, 3, 1, 4, 5], y = ['f', 'o', 'o', 'b', 'a', 'r']. I want to select the elements in y corresponding to elements in x that are greater than 1 and less than 5.

I tried

x = array([5, 2, 3, 1, 4, 5])
y = array(['f','o','o','b','a','r'])
output = y[x > 1 & x < 5] # desired output is ['o','o','b','a']

but this doesn't work. How would I do this?

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy