Matrix Transpose in Python

前端 未结 18 1208
甜味超标
甜味超标 2020-11-22 00:21

I am trying to create a matrix transpose function for python but I can\'t seem to make it work. Say I have

theArray = [[\'a\',\'b\',\'c\'],[\'d\',\'e\',\'f\         


        
18条回答
  •  鱼传尺愫
    2020-11-22 01:01

    The "best" answer has already been submitted, but I thought I would add that you can use nested list comprehensions, as seen in the Python Tutorial.

    Here is how you could get a transposed array:

    def matrixTranspose( matrix ):
        if not matrix: return []
        return [ [ row[ i ] for row in matrix ] for i in range( len( matrix[ 0 ] ) ) ]
    

提交回复
热议问题