8

To make my question clear say if I have an array a as Out[123]: [1, 3, 4, 6, 9, 10, 54] When I try to search the numbers in the list, searchsort returns correct value but when I try something not in the list, it returns an absurd value

here is some of the results

In [131]: a
Out[131]: [1, 3, 4, 6, 9, 10, 54]

In [132]: searchsorted(a,1)
Out[132]: 0

In [133]: searchsorted(a,6)
Out[133]: 3

In [134]: searchsorted(a,[9,54,1])
Out[134]: array([4, 6, 0])

In [135]: searchsorted(a,[9,54,1,0])
Out[135]: array([4, 6, 0, 0])
***> # here 0 is not in the list, but turns up @ position 0***

In [136]: searchsorted(a,740)
Out[136]: 7
***> # here 0 is not in the list, but turns up @ position 7***

why is this happening?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Ars3nous
  • 136
  • 1
  • 1
  • 8

4 Answers4

14

searchsorted tells you where the element belongs to guarantee ordering:

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.

inserting 740 at position 7 would preserve ordering, as would inserting 0 at position 0.

John Lyon
  • 11,180
  • 4
  • 36
  • 44
3

searchsorted doesn't tell you where things are, it tells you where things should go to keep the list sorted.

So 0 would have to be inserted at position 0, before the 1. Similarly, 740 needs to be inserted at position 7, beyond the current end of the list.

You can see this by reading the docs here:

numpy.searchsorted(a, v, side='left', sorter=None)

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.

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • It also can tell you where things that *are* in the index are, if the value you are looking for is located at the `'left'` index. Using binary search to determine containment is still the fastest method to do so. – Martijn Pieters Apr 19 '18 at 09:11
1

from the docs it states that it uses binary search to spot insertion point of an item in a sorted list.

the word 'insertion point' means, if item I is inserted to the insertion point index N in sorted array A, the array A will remain sorted with new item I.

your examples like [9, 54, 1] is meaningless since the array is not sorted.

you can use bisect module in python to do the same thing, without numpy.

thkang
  • 11,215
  • 14
  • 67
  • 83
0

searchsorted(initial_list,insert_list,side) default: side = 'left'

For example: searchsorted(x,v)
x = [1,2,3,4,5]
v = [-10,10,2,3]

This is my result of my example

:)

pmadhu
  • 3,373
  • 2
  • 11
  • 23
  • 2
    Please format the code sample and include the result as text in your answer. If imgur stops hosting the image in the future, readers will lose access to a significant part of your answer. – arowell Oct 13 '21 at 19:14