如何通過具有多個「標記」的另一個數組(標籤)獲取數組中值的索引(a)?例如,給定查找包含來自另一個數組的值之一的數組中的索引
label = array([1, 2])
a = array([1, 1, 2, 2, 3, 3])
的目標是找到的a
具有1或2的值的索引;也就是0,1,2,3。
我嘗試過幾種組合。以下都不起作用。
label = array([1, 2])
a = array([1, 1, 2, 2, 3, 3])
idx = where(a==label) # gives me only the index of the last value in label
idx = where(a==label[0] or label[1]) # Is confused by all or any?
idx = where(a==label[0] | label[1]) # gives me results as if nor. idx = [4,5]
idx = where(a==label[0] || label[1]) # syntax error
idx = where(a==bolean.or(label,0,1) # I know, this is not the correct form but I don`t remember it correctly but remember the error: also asks for a.all or a.any
idx = where(label[0] or label[1] in a) # gives me only the first appearance. index = 0. Also without where().
idx = where(a==label[0] or a==label[1]).all()) # syntax error
idx = where(a.any(0,label[0] or label[1])) # gives me only the first appearance. index=0. Also without where().
idx = where(a.any(0,label[0] | label[1])) # gives me only the first appearance. index=0. Also without where().
idx=where(a.any(0,label)) # Datatype not understood
好的,我想你會得到我的問題。有誰知道如何正確地做到這一點?最好是使用通用標籤而不是標籤[x]的解決方案,以便標籤的使用對於以後的更改更加可變。
您需要更準確地描述你想要做什麼。就目前而言,你的問題非常模糊。 – user2357112
不知道我是否理解你,但試試這個:'idx = where(a == label [0] | a == label [1])' –
在()中包裝這些'=='測試以獲得操作符的順序正確。 – hpaulj