The ZIP function accepts any number of iterated objects as parameters, wrapping the corresponding elements in an object into a
tuple, and then returns an iterator to the Zip object.
This iterative object can be used to list its elements in a circular way
If multiple iterations of an object have inconsistent lengths, the returned list is the same as the shortest-length iteration object.
Usage 1: Generate a Zip object with two lists
Example 1:
>>> a1=[1,2,3]>>> a2=[4,5,6]>>> a3=[7,8,9]>>> a4=["A", "B", "C", "D"]>>> zip1=zip (A1,A2,A3) >>> Print (ZIP1) <zip object at 0x7f5a22651c08>>>> for i in zip1: ... print (i) ... (1, 4, 7) (2, 5, 8) (3, 6, 9)
>>> zip2=zip (A1,A2,A4) >>> print (ZIP2) < zip object at 0x7f5a22651d48>>>> for j in zip2:... print (j) ... (1, 4, ' a ') (2, 5, ' B ') (3, 6, ' C ')
Example 3:
>>> zip3=zip (A4) >>> print (ZIP3) <zip object at 0x7f5a22651d08>>>>-i in ZIP3: ... pr int (i) ... (' A ',) (' B ',) (' C ',) (' d ',)
>>> zip4=zip (*a4) >>> >>> print (ZIP4) <zip object at 0x7f5a22651f08>>>> for J in Zip4: .... Print (j) ... (' A ', ' B ', ' C ', ' d ', ' A ', ' B ', ' C ', ' d ', ' A ', ' B ', ' C ', ' d ')
Usage 2: Two-dimensional matrix transformation (Matrix-column interchange)
>>> l1=[[1,2,3],[4,5,6],[7,8,9]]>>> Print ([[J[i] for J in L1] for I in range (len (l1[0))]) [[1, 4, 7], [ 2, 5, 8], [3, 6, 9]]>>> Zip (*l1) <zip object at 0x7f5a22651f88>>>> for I in Zip (*L1): ... print ( i) ... (1, 4, 7) (2, 5, 8) (3, 6, 9)
Python3 's Zip function