补充指数

问题描述 投票:0回答:2

我使用

np.where
来查找满足一组条件的数组的索引。有没有一种简单的方法可以找到不满足该条件的所有其他元素的索引?例如,如果我在数组
ok=np.where((condition1) & (condition2))
上使用
x
,我只能找到
x[ok]
。我如何找到
x
中不满足
ok
的所有元素?

python numpy
2个回答
1
投票

如果您单独存储条件本身,则可以将其否定:

true_indices = (condition1) & (condition2)
false_indices = ~true_indices

如果需要,您可以对这些值调用

np.where


1
投票

在Python中,布尔否定运算符是

~
。它会给你补充。

x = np.array([0, 1])
ok = np.where(x > 1)
not_ok = np.where(~(x > 1))
# you can also use this, if you already have ok
# where returns a tuple, the first index of which is the indices
not_ok = x[~ok[0]]
© www.soinside.com 2019 - 2024. All rights reserved.