将1的边框添加到python中的列表中

问题描述 投票:0回答:1
import numpy as np     
s=[[0, 0, 0, 0,0,0,0,0], [1, 1, 1, 1, 1,1,1, 0],[1, 1, 1, 1, 1,1,1, 0],[1,0,0,0,0,1,1,0], 
[0,1,1,1,0,0,0,0],[0,1,1,1,1,1,1,1],[0,1,1,1,1,1,1,1],[0, 0, 0, 0, 0, 0,0,0,0]]
k=np.pad(s,((1,1),(1,1)),mode='constant',constant_values=1)

ValueError: operands could not be broadcast together with remapped shapes [original->remapped]: (2,2) and requested shape (1,2)

我想填充边框为1的矩阵,但出现此错误

需要任何帮助

python-3.x numpy-broadcasting
1个回答
1
投票

您必须提供正确的输入矩阵! s的最后一行的长度为9,而其他所有长度均为8。

这里是您当前的“矩阵”。显然是错误的!

s = [[0, 0, 0, 0, 0, 0, 0, 0],
     [1, 1, 1, 1, 1, 1, 1, 0],
     [1, 1, 1, 1, 1, 1, 1, 0],
     [1, 0, 0, 0, 0, 1, 1, 0],
     [0, 1, 1, 1, 0, 0, 0, 0],
     [0, 1, 1, 1, 1, 1, 1, 1],
     [0, 1, 1, 1, 1, 1, 1, 1],
     [0, 0, 0, 0, 0, 0, 0, 0, 0]]
© www.soinside.com 2019 - 2024. All rights reserved.