簡體   English   中英

在numpy中的3D數組中查找2D數組的索引

[英]Find index of 2d array in a 3d array in numpy

我想在3d數組中找到2d數組。 為了在2d數組中找到1d數組,我可以使用np.where(np.all(a==b, axis=1))[0][0]

>>> import numpy as np
>>>
>>> a = np.array([[[1, 0, 0],
                   [0, 0, 0],
                   [0, 0, 0]],

                  [[0, 0, 0],
                   [0, 0, 0],
                   [0, 0, 0]]])
>>>
>>> b = np.array([[1, 0, 0],
                  [0, 0, 0],
                  [0, 0, 0]])
>>>
>>> a.find(b)
0

axis關鍵字接受元組,因此您可以簡單地執行以下操作:

np.where(np.all(a==b, axis=(1, 2)))[0][0]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM