簡體   English   中英

從此列的值中檢索 2d numpy 數組中的列索引

[英]Retrieve column index in 2d numpy array from the value of this column

如果我有兩個 numpy 數組,例如:

a = np.array([[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11]])
b = np.array([ 0, 4, 8])

我想獲得與b值對應的a列的索引。 這里應該是 0。

像這樣:

np.where(np.hsplit(a, 4) == b)

我能夠找到解決方案,但我認為這應該是一些更直觀的方法。

我不知道它是否或多或少直觀,但您可以轉置a並進行比較:

np.where( (a.transpose() == b  ).all(axis=1))

看看這個答案 唯一的區別是,你需要轉a

>>> a = np.array([[ 0,  1,  2,  3],
              [ 4,  5,  6,  7],
              [ 8,  9, 10, 11]])
>>> b = np.array([ 0, 4, 8])
>>> np.all(a.T==b,axis=1)
array([ True, False, False, False])
>>> np.where(np.all(a.T==b,axis=1))[0][0]
0

暫無
暫無

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

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