我试图将一维数组中的数据插入到二维数组中,并仍然保持二维数组的形状。
我下面的代码将二维数组重新格式化为一维。另外,为什么我现在有 26 个索引?我缺少什么?
import numpy as np
oneD_Array = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25])
twoD_Array = np.zeros((5, 5))
print(oneD_Array.shape)
print(oneD_Array)
print()
print()
print(twoD_Array.shape)
print(twoD_Array)
for i in range(len(oneD_Array), -1, -1):
# for subArray in twoD_Array:
twoD_Array = np.insert(oneD_Array, 0, [i])
print()
print(twoD_Array)
print(twoD_Array.shape)
输出是:
(25,)
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
25]
(5, 5)
[[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.]]
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
24 25]
(26,)
回首忆惘然
偶然的你
慕后森
相关分类