我有一个 numpy 数组
a = np.arange(30).reshape(5,6)
我想将它映射到
b = np.zeros((a.shape[0],a.shape[1]+2))
但将第一列和最后一列保留为零
即
b =
数组 [[0, 0, 1, 2, 3, 4, 5, 0],
。 。 。
[0, 24, 25, 26, 27, 28, 29, 0]])
谢谢
最佳答案
a = np.arange(30).reshape(5, 6)
b = np.zeros((a.shape[0], a.shape[1]+2), dtype=a.dtype)
b[:, 1:-1] = a
>>> b
array([[ 0, 0, 1, 2, 3, 4, 5, 0],
[ 0, 6, 7, 8, 9, 10, 11, 0],
[ 0, 12, 13, 14, 15, 16, 17, 0],
[ 0, 18, 19, 20, 21, 22, 23, 0],
[ 0, 24, 25, 26, 27, 28, 29, 0]])
关于python - 将数组映射到其他数组,开头和结尾均为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16941887/