gpt4 book ai didi

python - 将 Numpy 方阵数组转换为 numpy 方阵,保留 "layout"

转载 作者:太空宇宙 更新时间:2023-11-04 09:47:35 26 4
gpt4 key购买 nike

第一次在这里问问题。我有点“受阻”。

我有一个由 n x n 数组组成的数组(为简化起见,让 n=3):

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

(尽管我的数组包含的 3*3 数组多于 3 个)

我想实现这样的二维数组:

[0,1,2,9,10,11,18,19,20]  
[3,4,5,12,13,14,21,22,23]
[6,7,8,15,16,17,24,25,26]

有没有想不到的技巧,因为我想不出任何方法来完成转换。

谢谢

最佳答案

可能比 moveaxis 稍微干净一些:

import numpy as np
a = np.arange(27).reshape(3,3,3)
a.swapaxes(0,1).reshape(3,-1)
array([[ 0, 1, 2, 9, 10, 11, 18, 19, 20],
[ 3, 4, 5, 12, 13, 14, 21, 22, 23],
[ 6, 7, 8, 15, 16, 17, 24, 25, 26]])

关于python - 将 Numpy 方阵数组转换为 numpy 方阵,保留 "layout",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49161223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com