In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about the actual application operation scheme of Python matrix transposition and how to write code. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.
You need to make sure that the array has the same number of rows and columns. For example:
arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
The list recursion provides a simple method of matrix transposition:
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 is to use the zip function:
print map(list, zip(*arr))
This section provides two methods for Python matrix transpositions, one clear and simple, the other quick but somewhat obscure. Sometimes, data comes in the wrong way, for example, you use Microsoft's ADO interface to access the database, due to differences in language implementation between Python and MS. The Getrows method in Python may return column values, unlike the method names. The methods given in this section are common solutions to this problem, one clearer and one faster.
In the list recursion version, the inner recursion represents what to choose (row), and the outer recursion represents who to choose (column). When this process is complete, transpose is achieved. In the zip version, we pass a one-dimensional array to zip as an argument using the *arr syntax, and zip returns a tuple as the result. Then we use the list method on each tuple, producing a list of lists (i.e. matrices). Since we don't directly represent the zip result as a list, we can we can use itertools.izip to improve efficiency slightly
import itertools print map(list, itertools.izip(*arr))
However, in certain cases, the slight increase in efficiency cannot compensate for the increase in complexity. About *args and **kwds Syntax:*args(actually,* followed by variable name) syntax in Python means passing arbitrary positional variables. When you use this syntax (for example, when you define a function),Python binds this variable to a tuple and retains all positional information, not the specific variable. When you pass parameters using this method, variables can be arbitrary iterable objects.
** The kwds syntax is used in Python to receive named arguments. When you pass arguments in this way,Python binds variables to a dict, keeping all named arguments, not the specific variable values. When you pass arguments, the variable must be of type dict (or an expression that returns type dict).
If you want to transpose large arrays, use Numeric Python or other third-party packages that define methods that will make you dizzy. Description:
zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th
element
from each of the argument sequences. The returned list is
truncated
in length to the length of the shortest argument sequence.
After reading the above, do you have any further understanding of Python matrix transposition practical application operation scheme and how to write code? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.