【发布时间】:2022-11-14 05:04:59
【问题描述】:
我正在尝试使用np.zeros(n) 和dtype=object 初始化一个长度为n 的虚拟数组。我想使用这个虚拟数组来存储另一个长度为m 的数组的n 副本。
我试图避免 for 循环在每个索引处设置值。
我尝试使用下面的代码,但不断收到错误 -
temp = np.zeros(10, dtype=object)
arr = np.array([1.1,1.2,1.3,1.4,1.5])
res = temp * arr
期望的结果应该是 -
np.array([[1.1,1.2,1.3,1.4,1.5], [1.1,1.2,1.3,1.4,1.5], ... 10 copies])
我不断收到错误 -
operands could not be broadcast together with shapes (10,) (5,)
我知道出现此错误是因为编译器认为我正在尝试乘以这些数组。 那么我该如何完成任务呢?
【问题讨论】:
-
考虑使用 numpy.tile 函数numpy.org/doc/stable/reference/generated/numpy.tile.html
-
这有帮助!谢谢
标签: python numpy-ndarray