searchsorted¶
- searchsorted(x1: array, x2: array, /, *, side: Literal['left', 'right'] = 'left', sorter: array | None = None) array¶
Finds the indices into
x1such that, if the corresponding elements inx2were 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
sorterisNone, must be sorted in ascending order; otherwise,sortermust be an array of indices that sortx1in 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
vbe an element ofx2given byv = x2[j], wherejrefers to a valid index (see Indexing).If
vis less than all elements inx1, thenout[j]must be0.If
vis greater than all elements inx1, thenout[j]must beM, whereMis the number of elements inx1.Otherwise, each returned index
i = out[j]must satisfy an index condition:If
side == 'left', thenx1[i-1] < v <= x1[i].If
side == 'right', thenx1[i-1] <= v < x1[i].
Default:
'left'.sorter (Optional[array]) – array of indices that sort
x1in ascending order. The array must have the same shape asx1and 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
sortandargsort(i.e., if a value inx2is inserted intox1according to the corresponding index in the output array andsortis invoked on the resultant array, the sorted result should be an array in the same order).
New in version 2023.12.
Changed in version 2024.12: Fixed incorrect boundary conditions.