脾气暴躁的算术

ngBeginner

我有以下代码:

import numpy as np
x=np.array([[3, 5, 1]])
print(x.shape) #get (1,3)
np.multiply(x.shape, 8) #get [ 8, 24]

print(*x.shape) # get 1 3
np.array((np.multiply(*x.shape), 8)) #get [3, 8]

请解释为什么/如何np.multiply(* x.shape,8)得到[3,8]吗?

发生的事情是

np.multiply(*x.shape)

您正在(1,3)使用*运算符解压缩元组,并将每个元素作为参数传递给np.multiply因此结果1*3为3。

然后,您只是将结果包装到组成的数组中8,因此最终得到的数组是[3, 8]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章