在开头和结尾使用零将数组映射到另一个数组

时间:2013-06-05 13:56:19

标签: python arrays numpy reshape

我有一个numpy数组

a = np.arange(30).reshape(5,6)

我希望将其映射到

b = np.zeros((a.shape[0],a.shape[1]+2))

但将第一列和最后一列保留为零

b =

array [[0, 0, 1, 2, 3, 4, 5, 0], . . . [0, 24, 25, 26, 27, 28, 29, 0]])

由于

1 个答案:

答案 0 :(得分:1)

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]])