Fast matrix transposition in Python

前端 未结 3 1636
温柔的废话
温柔的废话 2020-12-06 14:38

Is there any fast method to make a transposition of a rectangular 2D matrix in Python (non-involving any library import).?

Say, if I have an array

         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-06 14:58

    >>> X = [1,2,3], [4,5,6]]
    >>> zip(*X)
    [(1,4), (2,5), (3,6)]
    >>> [list(tup) for tup in zip(*X)]
    [[1,4], [2,5], [3,6]]
    

    If the inner pairs absolutely need to be lists, go with the second.

提交回复
热议问题