【发布时间】:2012-09-21 23:49:06
【问题描述】:
有没有“扩展” numpy ndarray 的好方法?假设我有一个这样的 ndarray:
[[1 2]
[3 4]]
我希望每一行通过填充零来包含更多元素:
[[1 2 0 0 0]
[3 4 0 0 0]]
我知道必须有一些蛮力的方法来做到这一点(比如用零构造一个更大的数组,然后从旧的更小的数组中复制元素),只是想知道有没有 Pythonic 方法可以做到这一点。试过numpy.reshape但没用:
import numpy as np
a = np.array([[1, 2], [3, 4]])
np.reshape(a, (2, 5))
Numpy 抱怨说:ValueError: total size of new array must be unchanged
【问题讨论】:
标签: python numpy multidimensional-array