Zip function: It is the built-in function of Python (the built-in functions associated with the sequence are: sorted (), reversed (), enumerate (), zip ()), where sorted () and zip () return a sequence (list) object, reversed ( ), enumerate () returns an iterator (similar to a sequence)

Code One:

>>> name = ("Lilei","Lihua","Zhang")>>> age = (" +"," -"," the")>>> forA,ninchZip (name,age): ...PrintA,n...lilei21stLihua14Zhang34>>>

Code two:

>>> all={"hell": "helo":,"sony ":","lei": +} for in All.keys ():... Print 15>>>

Code three: If the length of the passed parameter is not equal, the length of the returned list is the same as the object with the shortest length in the parameter.

>>> AA = ("1","3","5")>>> BB = ("ee","TT")>>> forA,ninchZip (AA,BB): ...Printa,n ...1ee3TT>>>Zip (AA,BB) [('1','ee'), ('3','TT')]>>>

Code four: Zip () with the * operator, you can unzip the list object has been zip

>>>AA ('1','3','5')>>>BB ('ee','TT')>>> AD =Zip (AA,BB)>>> Zip (*AD) [('1','3'), ('ee','TT')]>>>Zip (AD) [('1','ee'),), (('3','TT'),)]>>>

Reference Address: http://www.cnblogs.com/BeginMan/archive/2013/03/14/2959447.html

Widening:

*Two-dimensional matrix transformation (Matrix-column interchange) For example, we have a two-dimensional matrix A that is described by a list= [[1, 2, 3], [4, 5, 6], [7, 8, 9] ] By using the Python list derivation method, we can also do this task easily.Print[[Row[col] forRowinchA forColinchRange (len (a[0))] [[1, 4, 7], [2, 5, 8], [3, 6, 9] Another confusing approach is to use the ZIP function:>>> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]>>> Zip (*a) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]>>> Map (List,zip (*a)) [[1, 4, 7], [2, 5, 8], [3, 6, 9] ] The ZIP function accepts any number of sequences as parameters, combining all sequences into one element in the same index as a new sequence of tuples merged into each sequence, and the length of the new sequence whichever is the shortest sequence in the parameter. In addition (*) operator with the ZIP function can achieve the opposite function of the zip, the merging sequence is split into multiple tuple. A new sequence of ①tuple>>>>x=[1,2,3],y=['a','b','C']>>>zip (x, y) [(1,'a'), (2,'b'), (3,'C']② The length of the new sequence is based on the shortest sequence in the parameter.>>>>x=[1,2],y=['a','b','C']>>>zip (x, y) [(1,'a'), (2,'b')]③ (*) operator with the ZIP function can achieve the opposite function of the zip, the merging sequence is split into multiple tuple. >>>>x=[1,2,3],y=['a','b','C']>>>>zip (*zip (x, y)) [(), ('a','b','C')]

Advanced Applications:

1. zip package unpacking list and multiples>>> a = [1, 2, 3]>>> B = ['a','b','C']>>> z =Zip (A, b)>>>z[(1,'a'), (2,'b'), (3,'C')]>>> Zip (*z) [(1, 2, 3), ('a','b','C')]2. Use zip to merge adjacent list items>>> a = [1, 2, 3, 4, 5, 6]>>> Zip (* ([ITER (a)] * 2))[(1, 2), (3, 4), (5, 6)]>>> group_adjacent =LambdaA, K:zip (* ([ITER (a)] *k))>>> Group_adjacent (A, 3)[(1, 2, 3), (4, 5, 6)]>>> Group_adjacent (A, 2)[(1, 2), (3, 4), (5, 6)]>>> Group_adjacent (A, 1)[(1,), (2,), (3,), (4,), (5,), (6,)]>>> Zip (a[::2], a[1::2])[(1, 2), (3, 4), (5, 6)]>>> Zip (A[::3], A[1::3], A[2::3])[(1, 2, 3), (4, 5, 6)]>>> group_adjacent =LambdaA, K:zip (* (A[I::K) forIinchrange (k)))>>> Group_adjacent (A, 3)[(1, 2, 3), (4, 5, 6)]>>> Group_adjacent (A, 2)[(1, 2), (3, 4), (5, 6)]>>> Group_adjacent (A, 1)[(1,), (2,), (3,), (4,), (5,), (6,)]3. Create a sliding window using zip and iterators (n-grams)>>> fromItertoolsImportIslice>>>defN_grams (A, n): ... z= (Islice (A, I, None) forIinchrange (n)) ...returnZip (*z) ...>>> a = [1, 2, 3, 4, 5, 6]>>> N_grams (A, 3)[(1, 2, 3), (2, 3, 4), (3, 4, 5), (4, 5, 6)]>>> N_grams (A, 2)[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]>>> N_grams (A, 4)[(1, 2, 3, 4), (2, 3, 4, 5), (3, 4, 5, 6)]4. Using the zip inversion dictionary>>> m = {'a': 1,'b': 2,'C': 3,'D': 4}>>>M.items () [('a', 1), ('C', 3), ('b', 2), ('D', 4)]>>>Zip (m.values (), M.keys ()) [(1,'a'), (3,'C'), (2,'b'), (4,'D')]>>> mi =dict (Zip (m.values (), M.keys ()))>>>mi{1:'a', 2:'b', 3:'C', 4:'D'}

Python (29) Powerful zip function