如果我运行以下命令:
import numpy as np
a = np.arange(9)
a = a.reshape((3,3))
我会得到这个:
a = [[0 1 2]
[3 4 5]
[6 7 8]]
如果我这样创建一个更大的数组:
b = np.zeros((5,5))
b = [[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
我如何有效地复制a
到b
这样的数组?
# border of 0 surrounding a to be filled in with other data later
b = [[ 0. 0. 0. 0. 0.]
[ 0. 0. 1. 2. 0.]
[ 0. 3. 4. 5. 0.]
[ 0. 6. 7. 8. 0.]
[ 0. 0. 0. 0. 0.]]
我正在寻找内置的功能(numpy
如果存在)。
您可以指定b[1:4, 1:4]
以表示零件:
>>> import numpy as np
>>> a = np.arange(9)
>>> a = a.reshape((3, 3))
>>> b = np.zeros((5, 5))
>>> b[1:4, 1:4] = a
>>> b
array([[ 0., 0., 0., 0., 0.],
[ 0., 0., 1., 2., 0.],
[ 0., 3., 4., 5., 0.],
[ 0., 6., 7., 8., 0.],
[ 0., 0., 0., 0., 0.]])
>>> b[1:4,1:4] = a + 1 # If you really meant `[1, 2, ..., 9]`
>>> b
array([[ 0., 0., 0., 0., 0.],
[ 0., 1., 2., 3., 0.],
[ 0., 4., 5., 6., 0.],
[ 0., 7., 8., 9., 0.],
[ 0., 0., 0., 0., 0.]])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句