Python's zip function

Source: Internet
Author: User

Python's zip function

Transfer from http://www.cnblogs.com/frydsh/archive/2012/07/10/2585370.html

The ZIP function accepts any number of sequences (including 0 and 1) as parameters, returning a tuple list. The specific meaning is not good to express in words, directly look at the example:

1. Example 1:

x = [1, 2, 3]y = [4, 5, 6]z = [7, 8, 9]xyz = Zip (x, y, z) print xyz

The result of the operation is:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)]

From this result, we can see the basic operation of the ZIP function.

2. Example 2:

x = [1, 2, 3]y = [4, 5, 6, 7]xy = Zip (x, y) print xy

The result of the operation is:

[(1, 4), (2, 5), (3, 6)]

From this result, we can see how the length of the zip function is handled.

3. Example 3:

x = [1, 2, 3]x = Zip (x) print X

The result of the operation is:

[(1,), (2,), (3,)]

From this result you can see how the ZIP function works when there is only one parameter.

4. Example 4:

x = Zip () print X

The result of the operation is:

[]

From this result, you can see how the zip function works without parameters.

5. Example 5:

x = [1, 2, 3]y = [4, 5, 6]z = [7, 8, 9]xyz = Zip (x, y, z) u = Zip (*xyz) print U

The result of the operation is:

[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

It is generally thought that this is a unzip process, and its operating mechanism is this:

Before running Zip (*xyz), the value of XYZ is: [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

Then, Zip (*xyz) is equivalent to zip ((1, 4, 7), (2, 5, 8), (3, 6, 9))

So the result is: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

Note: Using *list/tuple in a function call means separating the list/tuple, passing it as a positional parameter to the corresponding function (provided that the corresponding function supports an indefinite number of positional parameters)

6. Example 6:

x = [1, 2, 3]r = Zip (* [x] * 3) Print R

The result of the operation is:

[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

It operates in such a way that:

[x] generates a list of lists that have only one element x

[x] * 3 generates a list of lists, it has 3 elements, [x, X, X]

The meaning of Zip (* [x] * 3) is clear, zip (x, x, x)

Python's zip function

Related Article

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.