如何根据两个条件过滤数组A?
A = array([1, 2.3, 4.3, 10, 23, 42, 23, 12, 1, 1])
B = array([1, 7, 21, 5, 9, 12, 14, 22, 12, 0])
print A[(B < 13)] # here we get all items A[i] with i such that B[i] < 13
print A[(B > 5) and (B < 13)] # here it doesn't work
# how to get all items A[i] such that B[i] < 13
# AND B[i] > 5 ?
错误是:
ValueError:具有多个元素的数组的真值 不明确的。
答案 0 :(得分:5)
>>> A[np.logical_and(B > 5, B < 13)]
array([ 2.3, 23. , 42. , 1. ])
答案 1 :(得分:5)
您应该使用and
的运算符&
的按位(感谢@askewchan)版本。
即
print A[(B > 5) & (B < 13)]