NumPy Array Searching (Python Tutorial)

NumPy Array Searching

This is a detailed tutorial of the NumPy Array Search. Learn to search for different elements inside a NumPy Array with the help of examples.

Another way of accessing arrays when we want to search through an array in order to gain access to a particular element. In order to search through an array, we need to give in a certain value so that it could return the element at that particular index.

When we enter the elements which we want to locate and then we want to have those elements at those particular indexes, whoever makes a match with the index.

Searching NumPy Arrays

To search through an array, we have a method that will compare the indexes and get the element at that particular index. This method is known as where() method.

Let us go through an example to get clear with this concept:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([9,8,7,6,5,4,3,2,5]) 
#Here we use the method
array2=np.where(array1==5) 
# here we will print the arrays 
print(array2)

Output.

(array([4, 8]),)

So as per the output, we have five at two indexes in the whole array, and it will check through the whole array in order to find the matching elements, which means that five is present on indexes with numbers 8 and 4.

Let us take another example where we search for numbers which are divisible by 3 in the array:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([9,8,7,6,5,4,3,2,5]) 
#Here we use the method 
array2=np.where((array1%3)==0) 
# here we will print the arrays 
print(array2)

Output.

(array([0, 3, 6]),)

Here we get a result which states that we have numbers with an index of 0,3 and 6 who are divisible with 3.

Let us take another example where we will check for numbers which are divisible by 5:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([9,8,7,6,5,4,3,10,5]) 
#Here we use the method 
array2=np.where((array1%5)==0) 
# here we will print the arrays 
print(array2)

Output.

(array([4, 7, 8]),)

here we have numbers at indexes 4, 7 and 8 who are divisible by 5.

Sorting along with Search

When we need to sort our arrays along with the searches, we are going through using this method. In this method, we are performing a binary search in the array we are searching through, and this will return the index where the value which we will be specifying the value we want to insert. This also helps in maintaining the order of the search.

The name of this method is searchsorted()and this method is usually used on the sorted arrays.

Let us go through an example of this method:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([1,2,3,4,5]) 
#Here we use the method 
array2=np.searchsorted(array1, 5)
# here we will print the array
print(array2)

Output.

4

We see here that it will give us an index where the number 5 will be put into the array.

Now, what if we want to start our search from the right side of the array?

So by default, we always get the left-most index, but in order to get it from the right side we can give ‘side= ‘right”. Let us go through an example of this:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([4,5,6,7]) 
#Here we use the method 
array2=np.searchsorted(array1, 5, side='right') 
# here we will print the array 
print(array2)

Output.

2

So here this method will start searching from the right sides and will keep searching until number 5 is no longer less than the next value in the array.

Searching Multiple Values

We can also search for values at multiple indexes. So in order to search for more than one value, we use an array in which we will specify the values we want to search for.

Let us take an example to understand it in a better way:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([1,3,5,7,9]) 
#Here we use the method 
array2=np.searchsorted(array1,[2,4,6,8]) 
# here we will print the array 
print(array2)

Output.

[1 2 3 4]

Here as per the output, we know that we have to inert 2,4,6,8, at the indexes [1 2 3 4] in order to maintain the order of the array.

Let us take another example:

#importing the numpy package along with creating an alias 
import numpy as np 
#now we will create array 
array1=np.array([11,14,17,20]) 
#Here we use the method 
array2=np.searchsorted(array1,[12,13,15,16,18,19]) 
# here we will print the array 
print(array2)

Output.

[1 1 2 2 3 3]

Here we were getting the indexes as [1 1 2 2 3 3] where the given element should be inserted in order to maintain the order of the array and to keep it sorted.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *