Return 布尔数组中 "false" 个值的索引

Return the indices of "false" values in a boolean array

我觉得这是一个非常简单的问题,但我找不到解决方案。

给定一个 true/false 值的布尔数组,我需要输出所有值为“false”的索引。我有办法做到这一点:

测试=[真假真真]

test1 = np.where(test)[0]

这个returns[0,2,3],也就是说每个真值对应的索引。现在我只需要为 false 得到同样的东西,输出将是 [1]。有人知道怎么做吗?

enumerate:

>>> test = [True, False, True, True]
>>> [i for i, b in enumerate(test) if b]
[0, 2, 3]
>>> [i for i, b in enumerate(test) if not b]
[1]

使用 np.where(~test) 代替 np.where(test)

使用显式 np.array()

import numpy as np
test = [True, False, True, True]
a = np.array(test)
test1 = np.where(a==False)[0]    #np.where(test)[0]
print(test1)