http://blog.csdn.net/yongh701/article/details/50283689
In Python's numpy, the transpose of a two-dimensional array similar to array=[[1,2,3],[4,5,6],[7,8,9]], is a sentence array. T. In fact, no use of numpy, the simple use of Python, the code is not long, is also a line. Before this, however, the use of the map function and the zip (*) in Python are explained first.
First, the map function
First, the map function in Python is very simple. Handles each item in the second argument (typically an array) as the type of the first parameter. For example, the following code converts each item of a list to the STR type from the int type.
[Python]View PlainCopyprint?
- #-*-coding:utf-8-*-
- a=[1,2,3];
- Print map (str,a);
#-*-coding:utf-8-*-a=[1,2,3];p rint map (str,a);
The results of the operation are as follows:
In the following array, a one-dimensional array of each item in a two-dimensional array is summed, which is then naturally a one-dimensional array, since the one-dimensional array of each item is converted to an int.
[Python]View PlainCopyprint?
- #-*-coding:utf-8-*-
- a=[[1,3,4],[2,3,2];
- Print map (sum,a);
#-*-coding:utf-8-*-a=[[1,3,4],[2,3,2]];p rint map (sum,a);
The results of the operation are as follows:
Two, zip (*)
The use of zip has been described in "Python" using the zip function for Euclidean distance, cosine similarity (click to open link).
For example, the following section of code:
[Python]View PlainCopy print?
- #-*-coding:utf-8-*-
- x=[1,2,3];
- y=[4,5,6];
- z=[7,8,9];
- Print zip (x, y, z);
#-*-coding:utf-8-*-x=[1,2,3];y=[4,5,6];z=[7,8,9];p rint zip (x, y, z);
The results of the operation are as follows:
This means taking the item X of each list as an element in the one-dimensional array of the two-dimensional array returned by the X item.
In fact, the zip or in turn will be this two-dimensional array operation, but pay attention to write a zip (*), indicating that this is a zip inverse operation.
For example, the following section of code:
[Python]View PlainCopyprint?
- #-*-coding:utf-8-*-
- array=[[1,4,7],[2,5,8],[3,6,9];
- X,y,z=zip (*array);
- Print x, y, Z;
#-*-coding:utf-8-*-array=[[1,4,7],[2,5,8],[3,6,9]];x,y,z=zip (*array);p rint x, y, Z;
The value of XYZ, as shown below, is exactly the inverse of the zip function.
Third, the transpose of two-dimensional array in Python
How does this relate to array transpose?
Note that if zip (*array) does not go through X,y,z=zip (*array), split into X, Y, z three variables, then [[1,4,7],[2,5,8],[3,6,9]]; The result of the zip (*array) is exactly [(1, 2, 3), (4 , 5, 6), (7, 8, 9)], just to form a transpose relationship. This is the same for all array=[[1,2,3],[4,5,6],[7,8,9], and the two-dimensional array is the same, and you can try it without believing it.
Of course [(1, 2, 3), (4, 5, 6), (7, 8, 9)] is not the final result we need,
Because it is just a list of tuples, we want to keep the list as the same as the list, so we apply it to the map function above.
So for the transpose of an array, the code is as follows:
[Python]View PlainCopyprint?
- #-*-coding:utf-8-*-
- Array = [[1, 4], [2, 5], [3, 6]];
- Print map (list, zip (*array));
#-*-coding:utf-8-*-array = [[1, 4], [2, 5], [3, 6]];p rint map (list, zip (*array));
The results of the operation are as follows:
"Python" does not need to be numpy, use the map function and the zip (*) function to transpose the array (GO)