V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
HHH01
V2EX  ›  Python

Python 作业求教,矩阵转置

  •  
  •   HHH01 · Nov 8, 2019 · 4907 views
    This topic created in 2365 days ago, the information mentioned may be changed or developed.

    又有一题不是特别会做,求教大家, 需要用 python 写一个 function 去得出某个二维矩阵的转置矩阵。 好像看起来挺简单,转置矩阵就是 Nij=Nji,但是还没想到思路。因此求教大神。

    如题,

    Exercise 4. Within the module matrix_utils.py write a function transpose() . The function should accept one parameter matrix which is a two-dimensional matrix (in Python it is a list of lists of the same size). The function should return a transposed matrix. Example usages of this functions: matrix1 = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4] ] print("Transposed:", transpose(matrix1)) Transposed: [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]] matrix2 = [[ ' a ' , ' b ' ], [ ' c ' , ' d ' ] ] print("Transposed:", transpose(matrix2)) Transposed: [[ 'a ' , ' c '] , [ 'b ' , ' d ']]

    20 replies    2019-11-11 16:58:11 +08:00
    KylinJiang
        1
    KylinJiang  
       Nov 8, 2019
    emmm 不太在意性能的话:
    KylinJiang
        2
    KylinJiang  
       Nov 8, 2019
    map(list,zip(*N))
    N 为你的多维数组
    HHH01
        3
    HHH01  
    OP
       Nov 8, 2019
    @KylinJiang 不能用 zip,还没学到,用 lists 的 loop。。。
    HHH01
        4
    HHH01  
    OP
       Nov 8, 2019
    我想到的思路,但是有问题
    def transpose(matrix):
    matrix == list
    n = len(matrix)
    tran_m = []
    m=0
    for m in range(0,n):
    m += len(matrix[m])

    temp=[]
    for i in range(n):
    tran_m=tran_m+[temp]

    for j in range(m):
    temp=temp+[matrix[j][i]]

    return tran_m
    HHH01
        5
    HHH01  
    OP
       Nov 8, 2019
    想到了!
    def transpose(matrix):
    matrix == list

    tran_m = []

    for i in range(len(matrix[0])):
    temp=[]

    for j in range(len(matrix)):
    temp=temp+[matrix[j][i]]
    tran_m=tran_m+[temp]

    return tran_m
    cherbim
        6
    cherbim  
       Nov 8, 2019   ❤️ 1
    ```
    def transpose(m):
    result = []
    row = len(m)
    col = len(m[0])
    for i in range(col):
    item = []
    for index in range(row):
    item.append(m[index][i])
    result.append(item)
    return result


    matrix1 = [[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]]
    print("Transposed:", transpose(matrix1))
    ```
    cherbim
        7
    cherbim  
       Nov 8, 2019
    获得行数和列数,然后调一下行和列存入另一个矩阵
    TimePPT
        8
    TimePPT  
    PRO
       Nov 8, 2019
    进来学习。

    以及,每次看到这种题就感慨 numpy 真香 😂
    Trim21
        9
    Trim21  
       Nov 8, 2019 via Android
    @TimePPT import numpy 交作业可还行 233
    009694
        10
    009694  
       Nov 8, 2019
    t = [[1,2,3],[4,5,6]]
    t_t = [[row[i] for row in t] for i in range(max(len(t) for t in t))]
    009694
        11
    009694  
       Nov 8, 2019
    transpose = lambda t:[[row[i] for row in t] for i in range(max(len(t) for t in t))]
    TimePPT
        12
    TimePPT  
    PRO
       Nov 8, 2019
    @Trim21 老师让你用锤子锯子钳子做把椅子,你转身去库房拉了个数控机床😂
    ddzzhen
        13
    ddzzhen  
       Nov 8, 2019 via Android
    pandas 直接 T 就行了
    xiri
        14
    xiri  
       Nov 8, 2019
    emmmm,如果不是作业的话 numpy 分分钟就 over 了
    coordinate
        15
    coordinate  
       Nov 8, 2019
    for i in range(len(mat)):
    for j in range(i+1, len(mat[0])):
    mat[i][j], mat[j][i] = mat[j][i], mat[i][j]
    necomancer
        16
    necomancer  
       Nov 8, 2019
    traspose = lambda x : [ [x[i][j] for i in range(len(x))] for j in range(len(x[0]))]

    转置只能对 (mxn) 形状的列表有效。
    HHH01
        17
    HHH01  
    OP
       Nov 8, 2019 via Android
    @TimePPT 还没学到,不能用啊
    HHH01
        18
    HHH01  
    OP
       Nov 8, 2019 via Android
    还是众人拾柴火焰高
    cshlxm
        19
    cshlxm  
       Nov 9, 2019
    import numpy 哈哈哈
    list 的话,只能读一行,再循环每一行 append 一个对应数
    yuuiasuka
        20
    yuuiasuka  
       Nov 11, 2019
    可以参考下 numpy 里面的 transpose 方法是怎么实现的。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2757 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 13:09 · PVG 21:09 · LAX 06:09 · JFK 09:09
    ♥ Do have faith in what you're doing.