numpy.searchsorted() in Python
Last Updated :
10 Dec, 2018
numpy.searchsorted()
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, 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.
Code #1 : Working
Python
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print ("Input array : ", in_arr)
# the number which we want to insert
num = 4
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num)
print ("Output indices to maintain sorted array : ", out_ind)
Output :
Input array : [2, 3, 4, 5, 6]
The number which we want to insert : 4
Output indices to maintain sorted array : 2
Code #2 :
Python
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print ("Input array : ", in_arr)
# the number which we want to insert
num = 4
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num, side ='right')
print ("Output indices to maintain sorted array : ", out_ind)
Output :
Input array : [2, 3, 4, 5, 6]
The number which we want to insert : 4
Output indices to maintain sorted array : 3
Code #3 :
Python
# Python program explaining
# searchsorted() function
import numpy as geek
# input array
in_arr = [2, 3, 4, 5, 6]
print ("Input array : ", in_arr)
# the numbers which we want to insert
num = [4, 8, 0]
print("The number which we want to insert : ", num)
out_ind = geek.searchsorted(in_arr, num)
print ("Output indices to maintain sorted array : ", out_ind)
Output :
Input array : [2, 3, 4, 5, 6]
The number which we want to insert : [4, 8, 0]
Output indices to maintain sorted array : [2 5 0]
Similar Reads
numpy.sort() in Python numpy.sort() : This function returns a sorted copy of an array. Parameters : arr : Array to be sorted. axis : Axis along which we need array to be started. order : This argument specifies which fields to compare first. kind : [âquicksortâ{default}, âmergesortâ, âheapsortâ]Sorting algorithm. Return :
1 min read
Python | Pandas Index.searchsorted() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.Pandas Index.searchsorted() function find indices where elements should be inserted to
2 min read
numpy.sort_complex() in Python numpy.sort_complex() function is used to sort a complex array.It sorts the array by using the real part first, then the imaginary part. Syntax : numpy.sort_complex(arr) Parameters : arr : [array_like] Input array. Return : [complex ndarray] A sorted complex array. Code #1 : Python3 # Python program
1 min read
Python | Pandas Series.searchsorted() Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas searchsorted() is a method for sorted series. It allows user to pass values as
2 min read
sort() in Python sort() method in Python sort the elements of a list in ascending or descending order. It modifies the original list in place, meaning it does not return a new list, but instead changes the list it is called on. Example:Pythona = [5, 3, 8, 1, 2] a.sort() print(a) a.sort(reverse=True) print(a)Output[1
4 min read