这是我想要做的一个例子。我想拿b并将其添加到我的数组中,但是采用特定的格式(我只想知道执行此操作的步骤,我的代码处理字符串):
import numpy as np
a = np.zeros(25, dtype= np.character).reshape(5,5)
b = [1,2,3,4,5,6,7,8,9]
for i in b:
a[1:4,1:4] = i
print a
输出:
[['' '' '' '' '']
['' '9' '9' '9' '']
['' '9' '9' '9' '']
['' '9' '9' '9' '']
['' '' '' '' '']]
但我想要的是:
[['' '' '' '' '']
['' '1' '2' '3' '']
['' '4' '5' '6' '']
['' '7' '8' '9' '']
['' '' '' '' '']]
有谁能告诉我如何做到这一点?谢谢。
答案 0 :(得分:2)
import numpy as np
a = np.zeros(25, dtype= np.character).reshape(5,5)
b = np.array([1,2,3,4,5,6,7,8,9])
a[1:4,1:4] = b.reshape(3,3)
print(a)
产量
[['' '' '' '' '']
['' '1' '2' '3' '']
['' '4' '5' '6' '']
['' '7' '8' '9' '']
['' '' '' '' '']]