w3resource logo

:

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:

Improve this sample solution and post your code through Disqus