Introduction to the two-dimensional array in Python matrix transpose

Source: Internet
Author: User

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:

 
 
  1. arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]] 

The list recursion provides a simple matrix transpose method:

 
 
  1. print [[r[col] for r in arr] for col in range(len(arr[0]))]  
  2. [[1, 4, 7, 10], [2, 5, 8, 11], [3, 6, 9, 12]] 

Another faster and more advanced method, you can use the zip function:

 
 
  1. 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 ).

 
 
  1. import itertools  
  2. 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.