Python zip () function usage instance analysis, python instance analysis
This example describes how to use the Python zip () function. We will share this with you for your reference. The details are as follows:
The following describes how to use the zip () function in python:
>>> help(zip)Help on built-in function zip in module __builtin__:zip(...) zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the I-th element
From each of the argument sequences. The returned list is truncated
In length to the length of the shortest argument sequence.
zip([seq1, ...])Take a series of iteratable objects as parameters, package the corresponding elements in the objects into tuples, and return a list composed of these tuples. If the length of the input parameter is different, the length of the returned list is the same as the minimum length of the object in the parameter.
1
>>> x=[1,2,3]>>> y=[1,2,3]>>> z=(1,2,3)>>> zip(x,y,z)[(1, 1, 1), (2, 2, 2), (3, 3, 3)]
2
>>> X = (,) >>> y = [, 3] >>> zip (x, y) # length of the input parameter, the returned list length is the same as the minimum length of the object in the parameter [(1, 1), (2, 2), (3, 3)]
3
>>> x(1, 2, 3, 4)>>> zip(x)[(1,), (2,), (3,), (4,)]
4
>>> zip()[]
5 "zip () with the * operator, you can decompress the List objects that have been zip
>>> x=[1,2,3]>>> y=['a','b','c']>>> z=[4,5,6]>>> xyz=zip(x,y,z)>>> xyz[(1, 'a', 4), (2, 'b', 5), (3, 'c', 6)]>>> zip(*xyz)[(1, 2, 3), ('a', 'b', 'c'), (4, 5, 6)]
6
>>> X = [5, 6, 7] >>> [x] # [x] generates a list with only one element x [5, 6, 7]> [x] * 3 # [x] * 3 generate a list with three elements: [x, x, x] [[5, 6, 7], [5, 6, 7], [5, 6, 7] >>> x [5, 6, 7] >>> zip (* [x] * 3) # zip (* [x] * 3) is equivalent to zip (x, x, x) [(5, 5, 5), (6, 6, 6), (7, 7, 7)]
7
>>> name=['song','ping','python']>>> age=[26,26,27]>>> zip(name,age)[('song', 26), ('ping', 26), ('python', 27)]>>> for n,a in zip(name,age):... print n,a...song 26ping 26python 27