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)
The zip function in Python