"Python" does not need to be numpy, using the map function and the zip (*) function to transpose the array

Source: Internet
Author: User

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?
    1. #-*-coding:utf-8-*-
    2. a=[1,2,3];
    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?
    1. #-*-coding:utf-8-*-
    2. a=[[1,3,4],[2,3,2];
    3. 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?
    1. #-*-coding:utf-8-*-
    2. x=[1,2,3];
    3. y=[4,5,6];
    4. z=[7,8,9];
    5. 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?
    1. #-*-coding:utf-8-*-
    2. array=[[1,4,7],[2,5,8],[3,6,9];
    3. X,y,z=zip (*array);
    4. 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?
    1. #-*-coding:utf-8-*-
    2. Array = [[1, 4], [2, 5], [3, 6]];
    3. 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)

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.