Transferred from: http://www.cnblogs.com/BeginMan/archive/2013/03/14/2959447.html
This blog post is pretty good.
First, code guidance
First look at this section of code:
1 >>> name= (' Jack ', ' Beginman ', ' Sony ', ' Pcky ') 2 >>> age= (2001,2003,2005,2000) 3 >>> for A,n in Zip (name,age): 4 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 Pcky 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.< C4/>the returned list is truncated9 in length to the length of the shortest argument sequence.
Hint: Do not understand a certain number of help
Definition: Zip ([seql, ...]) Accepts a series of iterative objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples (lists). 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.
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 described by the list A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] We can also do this task easily by using the Python list derivation print [[Row[col] for R Ow in a] for Col in range (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 by the same index into an element that is a new sequence of tuples merged into each sequence, and 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 ')]③ (*) The function with the ZIP function allows you to reverse the functionality of the zip and split 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, 2), (3, 4), (5, 6)]>>> Gro Up_adjacent = lambda A, 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,)]>>> Zi P (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)]>>> Gro Up_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, 2) [(1, 2), (3, 4), (5, 6)]>>> group_adjacent (A, 1) [(1,), (2,), (3,), (4,), (5,), (6,)]3. Make Generate sliding windows (n-grams) >>> from itertools import ISLICE&G with zip and iteratorsT;>> 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, 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.valu Es (), 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 '}
Go python fragmented Knowledge (2): Powerful zip