As I told you in a comment to a previous answer, you need to use either:
c[a & b]
or
c[np.logical_and(a, b)]
The reason is that the and
keyword is used by Python to test between two booleans. How can an array be a boolean? If 75% of its items are True
, is it True
or False
? Therefore, numpy refuses to compare the two.
So, you either have to use the logical function to compare two boolean arrays on an element-by-element basis (np.logical_and
) or the binary operator &
.
Moreover, for indexing purposes, you really need a boolean array with the same size as the array you're indexing. And it has to be an array, you cannot use a list of True/False
instead:
The reason is that using a boolean array tells NumPy which element to return. If you use a list of True/False
, NumPy will interpret that as a list of 1/0
as integers, that is, indices, meaning that you' either get the second or first element of your array. Not what you want.
Now, as you can guess, if you want to use two boolean arrays a
or b
for indexing, choosing the items for which either a
or b
is True, you'd use
c[np.logical_or(a,b)]
or
c[a | b]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…