如果我运行以下内容:
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
中的函数。
[1..9]
是我的错误,但这个解决了我的问题。 - rlee827