If you do not know how to perform the next step on the transpose operation solution of the Python matrix transpose, You need to transpose a two-dimensional array to swap the rows and columns of the Python matrix transpose. in this way, you can complete the application operations you need. The following are the specific operations of the article.
You need to transpose a two-dimensional array to swap rows and columns. Discussion: Make sure that the number of rows and columns of the array is the same. For example:
- arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
The list recursion provides a simple matrix transpose method:
- print [[r[col] for r in arr] for col in range(len(arr[0]))]
- [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]]
Another faster and more advanced method, you can use the zip function:
- print map(list, zip(*arr))
This section provides two methods for Matrix transpose, one of which is clear and simple, and the other is fast but obscure. sometimes, the wrong method is used when data arrives. For example, if you use the Microsoft ADO interface to access the database, the Python matrix is transposed.
Different from MS in language implementation. the Getrows method may return column values in Python, which is different from the method name. the method provided in this section is a common solution to this problem, which is clearer and faster.
In the list recursive version, the inner recursive formula indicates the selected value (ROW), and the outer recursive formula indicates the selector (Column). After this process is completed, the transpose is implemented.
In the zip version, we use * arr syntax to pass the one-dimensional array to zip as the parameter. Then, zip returns a tuples as the result. then we use the list method for each tuples to generate a list (matrix ). because the zip result is not directly represented as list, we can use itertools. izip to slightly improve efficiency (because izip does not organize data in the memory as a list ).
- import itertools
- print map(list, itertools.izip(*arr))
However, under certain circumstances, the above method's slight improvement in efficiency cannot compensate for the increase in complexity. about * args and ** kwds Syntax: * args (in fact, * followed by the variable name) syntax in Python indicates passing any location variable, when you use this syntax (for example, you use it when defining a function), Python binds this variable to a tuples and keeps all location information, not specific variables. when you use this method to pass parameters, the variable can be any iteratable object (in fact, it can be any expression, as long as the return value is an iterator ).
** The kwds syntax is used in Python to receive named parameters. when you use this method to pass parameters, Python binds the variables to a dict and retains all named parameters, not specific variable values. when you pass a parameter, the variable must be of the dict type (or an expression whose return value is of the dict type ).
If you want to transpose a large array and use Numeric Python or other third-party packages, they define many methods, which will make you dizzy. the above article introduces the practical application solution of Python matrix transpose.