First, code guidance
First look at this section of code:
>>> name= (' Jack ', ' Beginman ', ' Sony ', ' Pcky ') >>> age= (2001,2003,2005,2000) >>> for a,n in zip (name,age): Print a,n 5 6 output: 7 Jack 2001 8 Beginman 2003 9 Sony 200510 Pcky 2000
Let's look at this section of code:
1 all={"Jack": 2001, "Beginman": 2003, "Sony": 2005, "Pcky": 2000}2 for I in All.keys (): 3 print i,all[i]4 5 output: 6 Sony 20057 P CKY 20008 Jack 20019 Beginman 2003
Find the difference between them?
The most obvious is: the first is concise, flexible, and can be entered sequentially.
Two, 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)
1 >>> type (sorted (s)) 2 <type ' list ' >3 >>> type (Zip (s)) 4 <type ' list ' >5 >>> type ( Reversed (s)) 6 <type ' listreverseiterator ' >7 >>> type (enumerate (s)) 8 <type ' Enumerate ' >
So what is the zip () function?
We help (Zip) to see:
1 >>> Help (Zip) 2 built-in function zip in module __builtin__:3 4 zip (...) 5 Zip (seq1 [, SEQ2 [...]]), [(Seq1[0], seq2[0] ...), (...) 6 7 Return A list of tuples, where each tuple contains the i-th element8 from each of the argument sequences. The returned list is truncated9 in length to the length of the shortest argument sequence.
Tip: Don't know a lot of help
The object can be iterated as a parameter , the object corresponding element packaged into tuple (tuple The is then returned by these tuples list . If the length of the passed-in parameter is not equal to
1 >>> z1=[1,2,3] 2 >>> z2=[4,5,6] 3 >>> result=zip (Z1,Z2) 4 >>> result 5 [(1, 4), (2, 5), (3, 6)] 6 >>> z3=[4,5,6,7] 7 >>> result=zip (Z1,Z3) 8 >>> result 9 [(1, 4), (2, 5), ( 3, 6)] >>>
The zip () Mate * operator allows you to unzip a list object that has already been zipped
1 >>> Zip (*result) 2 [(1, 2, 3), (4, 5, 6)]
More on the next level of understanding:
Content Source: http://www.cnblogs.com/diyunpeng/archive/2011/09/15/2177028.html (Blog Park talent really many!) )
* Two-dimensional matrix transformation (Matrix-column interchange) For example, we have a two-dimensional matrix a = [[1, 2, 3], [4, 5, 6], [7, which is described by the list, &NBSP;8,&NBSP;9]] We can do this easily with the Python list derivation method Print [ [row[col] for row in a] for col in range (Len (a[0]))][[1, 4, 7], [2, 5, 8], [3, &NBSP;6,&NBSP;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], The [3, 6, 9]] zip function takes any number of sequences as parameters, combining all the sequences into one element in the same index as a new sequence of tuples merged into each sequence, with the length of the new sequence whichever is the shortest sequence in the parameter. The addition of the (*) operator to the ZIP function enables the reverse function of the zip, which splits the merged sequence into multiple tuples. ①tuple's new sequence >>>>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 and zipA number of mates can implement a function that is contrary to zip, splitting the merged sequence into multiple tuples. >>>>x=[1,2,3],y=[' A ', ' B ', ' C ']>>>>zip (*zip (x, y)) [(' A ', ' B ', ' C ')]
Other 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,&NBSP;2), (3, 4), (5, 6)]>>> group_adjacent = lambda a, k: zip (* ([ITER (a)] * k)) >>> group_adjacent (A, &NBSP;3) [(1, 2, 3), (4, 5, 6)]>>> group_adjacent (a, 2) [(1,&NBSP;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 = lambda a, k: zip (* (a[i::k] for i in Range (k)) >>> group_adjacent (a, 3) [(1, 2, 3), (4, 5, 6)]>> > group_adjacent (A,&NBSP;2) [(1, 2), (3, 4), (5, 6)]>>> group_ Adjacent (a, 1) [(1,), (2,), (3,), (4,), (5,), (6,)]3. Use zip and iterators to generate sliding window ( N -grams) >>> from itertools import islice>>> def n_ Grams (a, n):... z = (Islice (a, i, none) for i in range (n)) ... return zip (*z) ...>>> a = [1, 2, 3, 4, 5, 6]>>> n_grams (A, 3) [(1, 2, 3), (2, &NBSP;3,&NBSP;4), &NBSP; (3, 4, 5), (4, 5, 6)]>>> n_grams (a, 2) [(1,&NBSP;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 zip function