更新时间:2022-11-23 22:12:26
您可以使用b扩展为2D阵列形状后进行乘法运算. indexing.html#numpy.newaxis"rel =" nofollow> None/np.newaxis
.然后,使用 .ravel
以获得所需的输出,就像这样-
You can use broadcasting
for multiplication after extending b
to a 2D array shape with None/np.newaxis
. Then, flatten the multiplication result with .ravel
for the desired output, like so -
(b[:,None]*a).ravel()
这实际上是在执行外部产品,因此也可以使用 np.outer
就像这样-
This is in effect performing outer product, so one can also use np.outer
like so -
np.outer(b,a).ravel()
样品运行-
In [822]: a
Out[822]: array([1, 2, 3, 4, 5])
In [823]: b
Out[823]: array([6, 7, 8])
In [824]: (b[:,None]*a).ravel()
Out[824]: array([ 6, 12, 18, 24, 30, 7, 14, 21, 28, 35, 8, 16, 24, 32, 40])
In [825]: np.outer(b,a).ravel()
Out[825]: array([ 6, 12, 18, 24, 30, 7, 14, 21, 28, 35, 8, 16, 24, 32, 40])