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?
2 Answers
a = np.array([[1,2],[3,4],[5,6]])
b = np.array([5,6])
np.where(np.all(a==b,axis=1))
1 Comment
Tuan Pham
Thank for your help!
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
Tuan Pham
Thank for your help!
searchforsortedlocation? Or is this some sort offind? Are you searching for items that are in the array; or place to put new ones?searchsortedon the 1st column. If you are worried about ties, calculate some function of the columns that resolves the issue.searchsortedonly makes sense if sorting of the array is well defined. It isn't a substitute for list index or find.Find the row indexes of several values in a numpy array.