Skip to content

Commit 480dbf6

Browse files
authored
Merge pull request numpy#27387 from HabibiHye/fix-searchsorted-doc
DOC: Clarify np.searchsorted documentation and add example for sorter
2 parents 9c51621 + 276c5fc commit 480dbf6

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

numpy/_core/fromnumeric.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,18 @@ def searchsorted(a, v, side='left', sorter=None):
15131513
>>> np.searchsorted([11,12,13,14,15], [-10, 20, 12, 13])
15141514
array([0, 5, 1, 2])
15151515
1516+
When `sorter` is used, the returned indices refer to the sorted
1517+
array of `a` and not `a` itself:
1518+
1519+
>>> a = np.array([40, 10, 20, 30])
1520+
>>> sorter = np.argsort(a)
1521+
>>> sorter
1522+
array([1, 2, 3, 0]) # Indices that would sort the array 'a'
1523+
>>> result = np.searchsorted(a, 25, sorter=sorter)
1524+
>>> result
1525+
2
1526+
>>> a[sorter[result]]
1527+
30 # The element at index 2 of the sorted array is 30.
15161528
"""
15171529
return _wrapfunc(a, 'searchsorted', v, side=side, sorter=sorter)
15181530

0 commit comments

Comments
 (0)