Fast matrix transposition in Python

前端 未结 3 1635
温柔的废话
温柔的废话 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 15:09

    If you're working with matrices, you should almost certainly be using numpy. This will perform numerical operations easier and more efficiently than pure Python code.

    >>> x = [[1,2,3], [4,5,6]]
    >>> x = numpy.array(x)
    >>> x
    array([[1, 2, 3],
           [4, 5, 6]])
    >>> x.T
    array([[1, 4],
           [2, 5],
           [3, 6]])
    

    "non-involving any library import" is a silly, non-productive requirement.

提交回复
热议问题