当numpy为多维数组(二维以上是),如何用按元素(比如坐标)查找时,如何获得行索引
vals = np.array([[0, 0],
[1, 0],
[2, 0],
[0, 1],
[1, 1],
[2, 1],
[0, 2],
[1, 2],
[2, 2],
[0, 3],
[1, 3],
[2, 3],
[0, 0],
[1, 0],
[2, 0],
[0, 1],
[1, 1],
[2, 1],
[0, 2],
[1, 2],
[2, 2],
[0, 3],
[1, 3],
[2, 3]])
方法1:
np.where((vals == (0, 1)).all(axis=1))
>>>(array([ 3, 15]),)
方法2:
(vals == (0, 1)).all(axis=1).nonzero()
>>>(array([ 3, 15]),)
方法3:暴力遍历
l_2d = [[1, 1], [0, 1], [0, 1], [0, 0], [1, 0], [1, 1], [1, 1]]
seen = [[1,1]]
for i, x in enumerate(l_2d):
if(x in seen):
print(i) # 此处打印与l_2d与seen中相同行的行号
ref:
https://stackoverflow.com/questions/25823608/find-matching-rows-in-2-dimensional-numpy-array
https://numpy.org/doc/stable/reference/generated/numpy.where.html