Example of zip function usage in Python and pythonzip Function

Source: Internet
Author: User

Example of zip function usage in Python and pythonzip Function

The zip function accepts any number of (including 0 and 1) sequences as parameters and returns a tuple list. The specific meaning is not easy to express in words. Let's look at the example:

1. Example 1:
Copy codeThe Code is as follows:
X = [1, 2, 3]
Y = [4, 5, 6]
Z = [7, 8, 9]
Xyz = zip (x, y, z)
Print xyz

The running result is:

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

The result shows the basic operation mode of the zip function.

2. Example 2:

Copy codeThe Code is as follows:
X = [1, 2, 3]
Y = [4, 5, 6, 7]
Xy = zip (x, y)
Print xy

The running result is:
Copy codeThe Code is as follows:
[(1, 4), (2, 5), (3, 6)]

The result shows the processing method of the zip function length.

3. Example 3:
Copy codeThe Code is as follows:
X = [1, 2, 3]
X = zip (x)
Print x

The running result is:
Copy codeThe Code is as follows:
[(1,), (2,), (3,)]

From this result, we can see that the zip function is run with only one parameter.

4. Example 4:
Copy codeThe Code is as follows:
X = zip ()
Print x

The running result is:
Copy codeThe Code is as follows:
[]

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

5. Example 5:

Copy codeThe Code is as follows:
X = [1, 2, 3]
Y = [4, 5, 6]
Z = [7, 8, 9]
Xyz = zip (x, y, z)
U = zip (* xyz)
Print u

The running result is:
Copy codeThe Code is as follows:
[(1, 2, 3), (4, 5, 6), (7, 8, 9)]

It is generally considered that this is an unzip process, and its operating mechanism is as follows:

Before Running zip (* xyz), the xyz value 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 ))

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

Note: In function calls, the * list/tuple method is used to separate list/tuple and pass it as a location parameter to the corresponding function (provided that the corresponding function supports the location parameter with an indefinite number)

6. Example 6:
Copy codeThe Code is as follows:
X = [1, 2, 3]
R = zip (* [x] * 3)
Print r

The running result is:
Copy codeThe Code is as follows:
[(1, 1, 1), (2, 2, 2), (3, 3, 3)]

Its operating mechanism is as follows:

[X] generates a list with only one element x

[X] * 3 generates a list with three elements: [x, x, x]

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

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.