MCPcopy Create free account
hub / github.com/numpy/numpy / searchsorted

Function searchsorted

numpy/core/fromnumeric.py:1333–1400  ·  view source on GitHub ↗

Find indices where elements should be inserted to maintain order. Find the indices into a sorted array `a` such that, if the corresponding elements in `v` were inserted before the indices, the order of `a` would be preserved. Assuming that `a` is sorted: ====== =========

(a, v, side='left', sorter=None)

Source from the content-addressed store, hash-verified

1331
1332@array_function_dispatch(_searchsorted_dispatcher)
1333def searchsorted(a, v, side='left', sorter=None):
1334 """
1335 Find indices where elements should be inserted to maintain order.
1336
1337 Find the indices into a sorted array `a` such that, if the
1338 corresponding elements in `v` were inserted before the indices, the
1339 order of `a` would be preserved.
1340
1341 Assuming that `a` is sorted:
1342
1343 ====== ============================
1344 `side` returned index `i` satisfies
1345 ====== ============================
1346 left ``a[i-1] < v <= a[i]``
1347 right ``a[i-1] <= v < a[i]``
1348 ====== ============================
1349
1350 Parameters
1351 ----------
1352 a : 1-D array_like
1353 Input array. If `sorter` is None, then it must be sorted in
1354 ascending order, otherwise `sorter` must be an array of indices
1355 that sort it.
1356 v : array_like
1357 Values to insert into `a`.
1358 side : {'left', 'right'}, optional
1359 If 'left', the index of the first suitable location found is given.
1360 If 'right', return the last such index. If there is no suitable
1361 index, return either 0 or N (where N is the length of `a`).
1362 sorter : 1-D array_like, optional
1363 Optional array of integer indices that sort array a into ascending
1364 order. They are typically the result of argsort.
1365
1366 .. versionadded:: 1.7.0
1367
1368 Returns
1369 -------
1370 indices : int or array of ints
1371 Array of insertion points with the same shape as `v`,
1372 or an integer if `v` is a scalar.
1373
1374 See Also
1375 --------
1376 sort : Return a sorted copy of an array.
1377 histogram : Produce histogram from 1-D data.
1378
1379 Notes
1380 -----
1381 Binary search is used to find the required insertion points.
1382
1383 As of NumPy 1.4.0 `searchsorted` works with real/complex arrays containing
1384 `nan` values. The enhanced sort order is documented in `sort`.
1385
1386 This function uses the same algorithm as the builtin python `bisect.bisect_left`
1387 (``side='left'``) and `bisect.bisect_right` (``side='right'``) functions,
1388 which is also vectorized in the `v` argument.
1389
1390 Examples

Callers

nothing calls this directly

Calls 1

_wrapfuncFunction · 0.85

Tested by

no test coverage detected