Python 为什么在本例中numpy.logical_和创建一个单例维度?

Python 为什么在本例中numpy.logical_和创建一个单例维度?,python,arrays,numpy,Python,Arrays,Numpy,我偶然发现了一些对我来说有点奇怪的东西。 考虑下面的代码: import numpy as np a = np.random.rand(6,7) print(a.shape) b = np.logical_and([a <= 1], [a >= 0]) print(b.shape) 但是a和b的形状不一样。我遗漏了什么?因为在使用方括号将布尔数组放入列表时添加了维度: [a <= 1] 注: 只需使用: b = np.logical_and(a <= 1, a &

我偶然发现了一些对我来说有点奇怪的东西。 考虑下面的代码:

import numpy as np

a = np.random.rand(6,7)
print(a.shape)

b = np.logical_and([a <= 1], [a >= 0])
print(b.shape)

但是a和b的形状不一样。我遗漏了什么?

因为在使用方括号将布尔数组放入列表时添加了维度:

[a <= 1]
注:

只需使用:

b = np.logical_and(a <= 1, a >= 0)

把括号弄丢了。出现单例DIM是因为under The hoods NumPy将输入转换为带有:np的数组。asarray[aGroup在运算符等效项中是必需的,b=a=0。在这个函数版本中它们是可以的。啊,我真傻!我没有考虑到您只在必要时使用括号。我会记住这一点。谢谢。
In [10]: np.array([a <= 1])
Out[10]:
array([[[ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True],
        [ True,  True,  True,  True,  True,  True,  True]]], dtype=bool)

In [11]: np.array([a <= 1]).shape
Out[11]: (1, 6, 7)
b = np.logical_and(a <= 1, a >= 0)