Suppose there is a matrix (two -dimensional list) of the size of 2x3:

>>> matrix = [[1, 2, 3], [4, 5, 6]]]

How can you change lines and columns in one line of code (scientifically: Transfer the matrix)?

>>> List (zip (*matrix))

Yu (1, 4), (2, 5), (3, 6) b.

Explanation. The asterisk before the value of the argument of the function unfolds the list in the list of arguments. Example:

>>> Print (*Matrix, Sep = ',')

Yu1, 2, 3sh, Yu4, 5, 6.

There were 2 elements in the Matrix list, and the Print call became similar:

>>> Print (matrix [0], matrix [1], sp = ',')

Yu1, 2, 3sh, Yu4, 5, 6.

Now zip. She takes the sequence to the input (one, two, more ...) and brings up a list of motorcade of the form:

[(matrix [0] [0] = 1, matrix [1] [0] = 4),

(matrix [1] [0] = 2, matrix [1] [1] = 5),

(matrix [2] [0] = 3, matrix [2] [1] = 6)]

But we need not a list of motorcies, but a list of lists - you reasonably object. Well! We apply to each element of the result of the List, it will turn it into the list:

>>> List (MAP (List, Zip (*Matrix)))

Yui1, 4sh, Yu2, 5sh, Yuz, Swim

This is the answer. By the way, the size of the matrix became 3x2.

And then add that you would not suffer from a bicycle building for transposing matrices and applied Numpy:

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

#machinelearning #artificialintelligence #ai #datascience #programming #Technology #Deeplearning