Signature: all(iterable, /)
Docstring:
Return True if bool(x) is True for all values x in the iterable.
对于一维数组:
In [200]: x=np.ones(3)
In [201]: x
Out[201]: array([1., 1., 1.])
In [202]: y = x==x
In [203]: y # 1d array of booleans
Out[203]: array([ True, True, True])
In [204]: bool(y[0])
Out[204]: True
In [205]: all(y)
Out[205]: True
对于二维数组:
In [206]: x=np.ones((2,3))
In [207]: x
Out[207]:
array([[1., 1., 1.],
[1., 1., 1.]])
In [208]: y = x==x
In [209]: y
Out[209]:
array([[ True, True, True],
[ True, True, True]])
In [210]: y[0]
Out[210]: array([ True, True, True])
In [211]: bool(y[0])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-211-d0ce0868392c> in <module>
----> 1 bool(y[0])
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
但对于不同的二维阵列:
In [212]: x=np.ones((3,1))
In [213]: y = x==x
In [214]: y
Out[214]:
array([[ True],
[ True],
[ True]])
In [215]: y[0]
Out[215]: array([ True])
In [216]: bool(y[0])
Out[216]: True
In [217]: all(y)
Out[217]: True
对numpy数组的迭代发生在第一个维度上。
[i for i in x]
每当在需要标量布尔值的上下文中使用多值布尔值数组时,都会引发此歧义值错误。
if
和
or/and
表达式是常见的。
In [223]: x=np.ones((2,3))
In [224]: y = x==x
In [225]: np.all(y)
Out[225]: True
np.all
是不同的巨蟒
all
因为它“知道”尺寸。在这种情况下,它会
ravel
要将数组视为1d:
缺省值(默认值)
axis
=
None
)是对输入数组的所有维度执行逻辑和。