w3resource

NumPy: Find elements within range from a given array of numbers

NumPy: Array Object Exercise-149 with Solution

Write a NumPy program to find elements within range from a given array of numbers.

Sample Solution:

Python Code:

import numpy as np
a = np.array([1, 3, 7, 9, 10, 13, 14, 17, 29])
print("Original array:")
print(a)
result = np.where(np.logical_and(a>=7, a<=20))
print("\nElements within range: index position")
print(result)

Sample Output:

Original array:
[ 1  3  7  9 10 13 14 17 29]

Elements within range: index position
(array([2, 3, 4, 5, 6, 7]),)

Pictorial Presentation:

NumPy: Find elements within range from a given array of numbers

Python Code Editor:

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

Previous: Write a NumPy program to copy data from a given array to another array.
Next: Write a NumPy program to swap columns in a given array.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Set comprehension:

>>> m = {x ** 2 for x in range(5)}
>>> m
{0, 1, 4, 9, 16}