2 回答

TA贡献1946条经验 获得超4个赞
您可以使用np.pad它,它还可以2-D使用指定填充宽度的值元组来填充数组,((top, bottom), (left, right)). 为此,您可以定义:
def pad_to_length(x, m):
return np.pad(x,((0, 0), (0, m - x.shape[1])), mode = 'constant')
用法
您可以从查找ndarray列数最多的开始。假设你有两个,a并且b:
a = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
b = np.array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
m = max(i.shape[1] for i in [a,b])
# 5
然后使用此参数来填充ndarrays:
pad_to_length(a, m)
array([[0, 1, 2, 0, 0],
[3, 4, 5, 0, 0],
[6, 7, 8, 0, 0]])

TA贡献1865条经验 获得超7个赞
我相信对此没有非常有效的解决方案。我认为您需要使用 for 循环遍历列表并单独处理每个数组:
for i in range(len(to_be_padded)):
padded = np.zeros((n, maxM))
padded[:,:to_be_padded[i].shape[1]] = to_be_padded[i]
to_be_padded[i] = padded
列表中maxM最长m的矩阵在哪里。
添加回答
举报