[英]Use of logical_and with list of lists
Suppose I have the following two lists:假设我有以下两个列表:
l = [[], [1]]
m = [0, 1]
If I check to see if elements are in a list:如果我检查元素是否在列表中:
>>> np.array(m[1]) == 1
True
>>> 1 in np.array(l)[1]
True
This works as expected.这按预期工作。
However, if I use the numpy logical_and
operator, this fails:但是,如果我使用 numpy
logical_and
运算符,则会失败:
>>> np.logical_and(np.array(m) == 1, 1 in np.array(l))
array([False, False])
Why are both positions in the array being evaluated as False
?为什么数组中的两个位置都被评估为
False
?
The goal is to evaluate lists of these forms element-wise.目标是按元素评估这些表单的列表。 And, as per the documentation,
np.logical_and
is used to "Compute the truth value of x1 AND x2 element-wise."而且,根据文档,
np.logical_and
用于“计算 x1 AND x2 元素的真值”。
Since l is a list of lists, I am using the in
operator to test the element-wise comparisons.由于 l 是一个列表列表,我使用
in
运算符来测试元素比较。
Thus, I expect the output of >>> np.logical_and(np.array(m) == 1, 1 in np.array(l))
因此,我希望输出
>>> np.logical_and(np.array(m) == 1, 1 in np.array(l))
to be成为
array([False, True])
after all, [1]
is just an element in np.array(l)
:毕竟,
[1]
只是np.array(l)
一个元素:
>>> for i in np.array(l):
... print(i)
...
[]
[1]
So just analyzing your code所以只需分析你的代码
np.array(m) == 1
>>> [False True]
1 in np.array(l)
>>> False
You're basically comparing False with [False True]你基本上是在比较False和[False True]
Update更新
If you want the output to be [False True] then you should use logical_or instead.如果您希望输出为 [False True],则应改用 logic_or。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.