Python NumPy: Select indices satisfying multiple conditions in a numpy array - w3resource
w3resource
Python Exercises

Python NumPy: Select indices satisfying multiple conditions in a numpy array

Python NumPy: Array Object Exercise-92 with Solution

Write a Python program to select indices satisfying multiple conditions in a numpy array.
Sample array:
a = np.array([97, 101, 105, 111, 117])
b = np.array(['a','e','i','o','u'])

Note: Select the elements from the second array corresponding to elements in the first array that are greater than 100 and less than 110

Sample Solution:-

Python Code:

import numpy as np
a = np.array([97, 101, 105, 111, 117])
b = np.array(['a','e','i','o','u'])
print("Original arrays")
print(a)
print(b)
print("Elements from the second array  corresponding to elements in the first array  that are greater than 100 and less than 110:")
print(b[(100 < a) & (a < 110)])

Sample Output:

Original arrays                                                        
[ 97 101 105 111 117]                                                  
['a' 'e' 'i' 'o' 'u']                                                  
Elements from the second array  corresponding to elements in the first 
array  that are greater than 100 and less than 110:                    
['e' 'i']

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to remove all rows in a numpy array that contain non-numeric values.
Next: Write a Python program to get the magnitude of a vector in numpy.

What is the difficulty level of this exercise?