假设我有一个 numpy 数组x = [5, 2, 3, 1, 4, 5],y = ['f', 'o', 'o', 'b', 'a', 'r']. 我想选择大于1且小于5的元素y对应的元素。x
x = [5, 2, 3, 1, 4, 5]
y = ['f', 'o', 'o', 'b', 'a', 'r']
y
x
我试过了
x = array([5, 2, 3, 1, 4, 5]) y = array(['f','o','o','b','a','r']) output = y[x > 1 & x < 5] # desired output is ['o','o','a']
但这不起作用。我该怎么做?
如果添加括号,则表达式有效:
>>> y[(1 < x) & (x < 5)] array(['o', 'o', 'a'], dtype='|S1')