Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to carry out the actual operation of two-dimensional array in Python matrix transpose

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to carry out the Python matrix transpose of the two-dimensional array of the actual operation, the editor feels very practical, so share with you to learn, I hope you can get something after reading this article, say no more, follow the editor to have a look.

If you don't know how to do the next step in the practical application of Python matrix transpose, you need to transpose a two-dimensional array to interchange the rows and rows of Python matrix transpose. In this way, you can complete the application operation you need.

You need to transpose a two-dimensional array, swap rows, and discuss: you need to make sure that the number of rows and rows in the array is the same. For example:

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

List recursion provides a simple method of matrix transpose:

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

For a 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 is clear and simple, the other is fast but somewhat obscure. Sometimes, the data arrives in the wrong way, for example, you use Microsoft's ADO interface to access the database, due to Python matrix transposition

And MS in the language implementation of the difference. The Getrows method may return a column value in Python, which is different from the name of the method. The solution given in this section is a common solution to this problem, one clearer and the other faster.

In the recursive version of the list, the inner recursion indicates what to choose (row), and the outer recursion represents the selector (column). After this process is completed, the transposition is realized.

In the zip version, we use the * arr syntax to pass an one-dimensional array to zip as a parameter, and then zip returns a tuple as a result. Then we use the list method for each tuple to produce a list of lists (that is, matrices). Because we don't directly represent the results of zip as list, we can use itertools.izip to slightly improve efficiency (because izip doesn't organize data into lists in memory).

Import itertools print map (list, itertools.izip (* arr))

However, in certain cases, the slight improvement in efficiency of the above method can not make up for the increase in complexity. About * args and * * kwds syntax: * args (in fact, the * sign is followed by a variable name) syntax means to pass an arbitrary position variable in Python. When you use this syntax (for example, when you define a function), Python binds this variable to a tuple and retains all the location information, not the specific variable. When you use this method to pass parameters, the variable can be any iterable object (actually any expression, as long as the return value is an iterator).

* * kwds syntax is used in Python to receive named parameters. When you pass parameters in this way, Python binds the variable to a dict, keeping all named parameters instead of specific variable values. When you pass parameters, the variable must be of type dict (or an expression that returns a type of dict).

If you want to transpose a large array, use Numeric Python or other third-party packages, which define a lot of methods that will make you dizzy.

The above is how to carry out the actual operation of the two-dimensional array in the Python matrix transpose. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report