Skip to content

Commit a4d7e43

Browse files
committed
Clarified np.searchsorted documentation and added example for sorter
1 parent b662c52 commit a4d7e43

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

numpy/_core/fromnumeric.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1535,6 +1535,16 @@ def searchsorted(a, v, side='left', sorter=None):
15351535
>>> np.searchsorted([11,12,13,14,15], [-10, 20, 12, 13])
15361536
array([0, 5, 1, 2])
15371537
1538+
When `sorter` is used, the returned indices refer to the sorted array of 'a' and not a itself:
1539+
1540+
>>> a = np.array([40, 10, 20, 30])
1541+
>>> sorter = np.argsort(a)
1542+
>>> sorter
1543+
array([1, 2, 3, 0]) # Indices that would sort the array
1544+
>>> np.searchsorted(a, 25, sorter=sorter)
1545+
2
1546+
>>> a[sorter[np.searchsorted(a, 25, sorter=sorter)]]
1547+
30 # The element at index 2 of the sorted array is 30.
15381548
"""
15391549
return _wrapfunc(a, 'searchsorted', v, side=side, sorter=sorter)
15401550

0 commit comments

Comments
 (0)