Introduction
The zip() function is used to take an iterable object as a parameter, pack the corresponding elements in the object into tuples, and then return a list of these tuples.
ps. If the number of elements in each iterator is inconsistent, the length of the returned list is the same as the shortest object. Using the * operator, the tuple can be decompressed into a list.
example
a =[1,2,3]
b =[4,5,6]
c =[4,5,6,7,8]
zipped =zip(a,b) #Packed as a list of tuples
# Output:[(1,4),(2,5),(3,6)]zip(a,c) #The number of elements is consistent with the shortest list
# Output:[(1,4),(2,5),(3,6)]zip(*zipped) #Contrary to zip,*zipped can be understood as decompression and return to a two-dimensional matrix
[(1,2,3),(4,5,6)]
The zip function accepts any number of (including 0 and 1) sequences as parameters and returns a list of tuples.
Look directly at the example:
code show as below:
x =[1,2,3]
y =[4,5,6]
z =[7,8,9]
xyz =zip(x, y, z)
print xyz
The result of running is:
[(1,4,7),(2,5,8),(3,6,9)]
From this result, we can see the basic operation of the zip function.
code show as below:
x =[1,2,3]
y =[4,5,6,7]
xy =zip(x, y)
print xy
The result of running is:
code show as below:
[(1,4),(2,5),(3,6)]
From this result, we can see how the length of the zip function is handled.
code show as below:
x =[1,2,3]
x =zip(x)
print x
The result of running is:
code show as below:
[(1,),(2,),(3,)]
From this result, we can see how the zip function works when there is only one parameter.
code show as below:
x =zip()
print x
The result of running is:
code show as below:
[]
From this result, we can see how the zip function works without parameters.
code show as below:
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 running is:
code show as below:
[(1,2,3),(4,5,6),(7,8,9)]
It is generally believed that this is an unzip process, and its operating mechanism is like 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 running result is: [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
Note: The use of *list/tuple in function calls means to separate list/tuple and pass them to the corresponding function as positional parameters (provided that the corresponding function supports an indefinite number of positional parameters)
code show as below:
x =[1,2,3]
r =zip(*[x]*3)
print r
The result of running is:
code show as below:
[(1,1,1),(2,2,2),(3,3,3)]
Its operating mechanism is like this:
[ x] generates a list of lists with 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)
So far, this article on how to use the zip function in Python is introduced. For more related content of the zip function in Python, please search for the previous article of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou more in the future. .Cn!
Recommended Posts