2016-08-15 39 views
2

我想索引像函数:np.searchsorted([1,2,3,4,5], 3)返回2,但万一二维:np.searchsorted([[1,2],[3,4],[5,6]], [5,6])应该返回2.我该怎么做?使用搜索排序的二维数组

+0

你真的做了'sorted'位置'search'?或者这是某种“发现”?你在搜索数组中的项目吗?或放置新的? – hpaulj

+0

只要在第一列上搜索'searchs'。如果您担心关系,请计算解决问题的列的某些功能。 'searchsorted'只有在数组的排序定义良好时才有意义。它不能替代列表索引或查找。 – hpaulj

+0

对于多个值,你可以看看这里:['找到numpy数组中的几个值的行索引](http://stackoverflow.com/questions/38674027/find-the-row-indexes-of-几个值-IN-A-numpy的阵列)。 – Divakar

回答

1

documentation for searchsorted表示它只适用于一维阵列。

你可以在使用内置的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 
+0

感谢您的帮助! –

3
a = np.array([[1,2],[3,4],[5,6]]) 
b = np.array([5,6]) 
np.where(np.all(a==b,axis=1)) 
+0

感谢您的帮助! –