I would like to have the index like function :np.searchsorted([1,2,3,4,5], 3)
returns 2 but in case 2-D dimension: np.searchsorted([[1,2],[3,4],[5,6]], [5,6])
should return 2. How can I do that?
Asked
Active
Viewed 691 times
2

Tuan Pham
- 49
- 4
-
Are you really doing `search` for `sorted` location? Or is this some sort of `find`? Are you searching for items that are in the array; or place to put new ones? – hpaulj Aug 15 '16 at 03:45
-
Just do `searchsorted` on the 1st column. If you are worried about ties, calculate some function of the columns that resolves the issue. `searchsorted` only makes sense if sorting of the array is well defined. It isn't a substitute for list index or find. – hpaulj Aug 15 '16 at 06:48
-
For multiple values, you could take a look here : [`Find the row indexes of several values in a numpy array`](http://stackoverflow.com/questions/38674027/find-the-row-indexes-of-several-values-in-a-numpy-array). – Divakar Aug 15 '16 at 09:15
-
Thank for your help! – Tuan Pham Aug 15 '16 at 14:16
2 Answers
1
The documentation for searchsorted
indicates that it only works on 1-D arrays.
You can find the index location in a list using built-in methds:
>>> a = [[1,2],[3,4],[5,6]]
>>> a.index([5,6])
2
>>> a = [[1,2],[5,6],[3,4]]
>>> print(a.index([5,6]))
>>> a.sort()
>>> print(a.index([5,6]))
1
2

James
- 32,991
- 4
- 47
- 70