2

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?

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? Commented Aug 15, 2016 at 3: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. Commented Aug 15, 2016 at 6:48
  • For multiple values, you could take a look here : Find the row indexes of several values in a numpy array. Commented Aug 15, 2016 at 9:15
  • Thank for your help! Commented Aug 15, 2016 at 14:16

2 Answers 2

3
a = np.array([[1,2],[3,4],[5,6]])
b = np.array([5,6])
np.where(np.all(a==b,axis=1))
Sign up to request clarification or add additional context in comments.

1 Comment

Thank for your help!
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

1 Comment

Thank for your help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.