29 Mar Search a Numpy Array for a value
In a Numpy array, we can search for a specific value using the numpy.where() method. This method returns the index where the specific element is found.
Let us see an example to search an array for a value in Numpy:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import numpy as np # Create a Numpy Array n = np.array([10, 20, 30, 40, 50, 30]) print("Iterating array...") for a in n: print(a) # Searching a specific value 30 res = np.where(n == 30) print("\nElement 30 found at following indexes:") print(res) |
Output
1 2 3 4 5 6 7 8 9 10 11 12 |
Iterating array... 10 20 30 40 50 30 Element 30 found at following indexes: (array([2, 5], dtype=int64),) |
The out above displays [2, 5] that means the value 30 found at index 2 and 5.
If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.
For Videos, Join Our YouTube Channel: Join Now
Read More:
No Comments