【问题标题】:Is there a way to change axis of an 2d array in Python? [duplicate]有没有办法在 Python 中更改二维数组的轴? [复制]
【发布时间】:2020-07-29 00:07:43
【问题描述】:

我想改变矩阵的xy 轴。例如我想存储每个的第一个元素 第一行中的嵌套数组,第二行中每个嵌套数组的第二个等... 例如:

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

我想改成这样:

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

注意:这不是轮换

【问题讨论】:

  • 要转置矩阵吗?试试list(map(list, zip(*l)))

标签: python arrays matrix


【解决方案1】:
import numpy as np

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

# convert the list of lists to an array
data = np.array(data)

# transpose the array
data_t = data.T

# print(data_t)
array([[ 1,  7, 13, 19],
       [ 2,  8, 14, 20],
       [ 3,  9, 15, 21],
       [ 4, 10, 16, 22],
       [ 5, 11, 17, 23],
       [ 6, 12, 18, 24]])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多