In this blog, I will show you how to search a specific item in an array.
Search
NumPy has a method called where
to find an item in array. This method requires an expression for its parameter. Let me show you how to implement it.
First, let us create an array
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 2])
Then, use method where
to find the index of an item in array and store it in a variable x
x = np.where(arr == 2)
Let us print the value of x
to see its output
print(x)
The following is the output
(array([1, 5]),)
From the output above, you can see that 2
is occurs two times in index 1
and 5
.
Congratulations, that is how you can search an item in NumPy array. Thank you for reading, and have a nice day!
Top comments (0)