Open In App

Searching in a NumPy array

Last Updated : 01 Oct, 2020
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Numpy provides various methods for searching different kinds of numerical values, in this article, we will cover two important ones.

  • numpy.where()
  • numpy.searchsorted()

1. numpy.where:() It returns the indices of elements in an input array where the given condition is satisfied.

Syntax: numpy.where(condition[, x, y]) 

Parameters:

  • condition : When True, yield x, otherwise yield y.
  • x, y : Values from which to choose. x, y and condition need to be broadcastable to some shape.

Returns:
out : [ndarray or tuple of ndarrays] If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.

If only condition is given, return the tuple condition.nonzero(), the indices where condition is True.

The following example demonstrates how to search using where().

Python3




# importing the module
import numpy as np
  
# creating the array
arr = np.array([10, 32, 30, 50, 20, 82, 91, 45])
  
#  printing arr
print("arr = {}".format(arr))
  
#  looking for value 30 in arr and storing its index in i
i = np.where(arr == 30)
print("i = {}".format(i))


Output:

arr = [10 32 30 50 20 82 91 45]
i = (array([2], dtype=int64),)

As you can see variable i is an iterable with the index of our searched value as the first element.  We can make it look better by replacing the last print statement with

print("i = {}".format(i[0]))

This will change the final output to

arr = [10 32 30 50 20 82 91 45]
i = [2]

2. numpy.searchsorted(): The function is used to find the indices into a sorted array arr such that, if elements are inserted before the indices, the order of arr would be still preserved. Here, a binary search is used to find the required insertion indices.

Syntax : numpy.searchsorted(arr, num, side=’left’, sorter=None)

Parameters :

  • arr : [array_like] Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it.
  • num : [array_like]The Values which we want to insert into arr.
  • side : [‘left’, ‘right’], optional.If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of a).
  • num : [array_like, Optional] array of integer indices that sort array a into ascending order. They are typically the result of argsort.

Return : [indices], Array of insertion points with the same shape as num.

The following example explains the use of searchsorted().

Python3




# importing the module
import numpy as np
  
# creating the array
arr = [1, 2, 2, 3, 3, 3, 4, 5, 6, 6]
print("arr = {}".format(arr))
  
# left-most 3
print("left-most index = {}".format(np.searchsorted(arr, 3, side="left")))
  
# right-most 3
print("right-most index = {}".format(np.searchsorted(arr, 3, side="right")))


Output:

arr = [1, 2, 2, 3, 3, 3, 4, 5, 6, 6]
left-most index = 3
right-most index = 6


Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg