2016-07-27 107 views
0

列说,我有一个数组如何找到一个ndarray

x = array([[ 0, 1, 2, 5], 
     [ 3, 4, 5, 5], 
     [ 6, 7, 8, 5], 
     [ 9, 10, 11, 5]]) 

我需要找到[3, 4, 5, 5]位置/索引。在这种情况下,它应该返回1

回答

0

创建一个数组y,该数组的行数等于您正在查找的行数。然后,做一个元素比较x == y,并找到你得到的所有行True

import numpy as np 

x1 = np.array([[0, 1, 2, 5], [3, 4, 5, 5], 
       [6, 7, 8, 5], [9, 10, 11, 5]]) 
y1 = np.array([[3, 4, 5, 5]] * 4) 
print(np.where(np.all(x1 == y1, axis=1))[0]) # => [1] 

该方法返回所需行出现的索引数组。

y2 = np.array([[1, 1, 1, 1]] * 4) 
print(np.where(np.all(x1 == y2, axis=1))[0]) # => [] 

x2 = np.array([[3, 4, 5, 5], [3, 4, 5, 5], 
       [6, 7, 8, 5], [9, 10, 11, 5]]) 
print(np.where(np.all(x2 == y1, axis=1))[0]) # => [0 1]