如何通过具有多个“标记”的另一个数组(标签)获取数组中值的索引(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