使用searchsorted用于2D数组

时间:2016-08-15 01:01:44

标签: python numpy

我希望像函数一样使用索引:np.searchsorted([1,2,3,4,5], 3)返回2但是如果二维维度:np.searchsorted([[1,2],[3,4],[5,6]], [5,6])应该返回2.我该怎么做?

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))

答案 1 :(得分:1)

documentation for searchsorted表示它仅适用于1-D阵列。

您可以使用内置的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