簡體   English   中英

numpy where:如何通過匹配一維數組在 3D 數組中找到二維數組的索引?

[英]numpy where: how to find a 2D array's index in a 3D array by matching a 1D array?

a = np.array([[[1,2,3], (1,1,0,0,8)], [[1,2,3], (1,1,2,0,8)], [[1,2,3], (4,1,0,0,8)]])
where = np.where(a[:,1] == (1,1,0,0,8))
print(where)

輸出:

(array([], dtype=int64),)

我想要它 output 0 ,出現(1,1,0,0,8)的行的索引

NumPy 通常將元組解釋為數組或數組的子集而不是元素,但您可以通過以下方式繞過它:

el = np.array([None], dtype = object)
el[0] = (1,1,0,0,8)

where = np.where( a[:,1] == el ) # outputs a tuple: (array([0], dtype=int64),)

暫無
暫無

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

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