searchsorted¶
- searchsorted(x1: array, x2: array, /, *, side: Literal['left', 'right'] = 'left', sorter: array | None = None) array ¶
Finds the indices into
x1
such that, if the corresponding elements inx2
were inserted before the indices, the order ofx1
, when sorted in ascending order, would be preserved.- Parameters:
x1 (array) – input array. Must be a one-dimensional array. Should have a real-valued data type. If
sorter
isNone
, must be sorted in ascending order; otherwise,sorter
must be an array of indices that sortx1
in ascending order.x2 (array) – array containing search values. Should have a real-valued data type.
side (Literal['left', 'right']) –
argument controlling which index is returned if a value lands exactly on an edge.
Let
x
be an array of rankN
wherev
is an individual element given byv = x2[n,m,...,j]
.If
side == 'left'
, theneach returned index
i
must satisfy the index conditionx1[i-1] < v <= x1[i]
.if no index satisfies the index condition, then the returned index for that element must be
0
.
Otherwise, if
side == 'right'
, theneach returned index
i
must satisfy the index conditionx1[i-1] <= v < x1[i]
.if no index satisfies the index condition, then the returned index for that element must be
N
, whereN
is the number of elements inx1
.
Default:
'left'
.sorter (Optional[array]) – array of indices that sort
x1
in ascending order. The array must have the same shape asx1
and have an integer data type. Default:None
.
- Returns:
out (array) – an array of indices with the same shape as
x2
. The returned array must have the default array index data type.
Notes
For real-valued floating-point arrays, the sort order of NaNs and signed zeros is unspecified and thus implementation-dependent. Accordingly, when a real-valued floating-point array contains NaNs and signed zeros, what constitutes ascending order may vary among specification-conforming array libraries.
While behavior for arrays containing NaNs and signed zeros is implementation-dependent, specification-conforming libraries should, however, ensure consistency with
sort
andargsort
(i.e., if a value inx2
is inserted intox1
according to the corresponding index in the output array andsort
is invoked on the resultant array, the sorted result should be an array in the same order).New in version 2023.12.