The use of the zip () function in Python

Source: Internet
Author: User
Tags python list

definition:zip ([iterable, ...])
Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, packages the corresponding elements in the object into tuple (tuples), and then returns a list of these tuples. 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. Using the * operator, you can unzip the list (decompression), see the following example to understand:

 >>> a = [1,2,3,4 >>> b = [5,6,7,8]  >>> c = [5,6,7,8,9,10 >>> test_zip = Zip (A, b)  >>> test_zip[( 1, 5), (2, 6), (3, 7), (4, 8)]  >>> test_zip1 = Zip (a,c)  >>> test_zip1[( 1, 5), (2, 6), (3, 7), (4, 8)]  >>> test_zip2 = Zip (b,c)  >>> test_zip2[( 5, 5), (6, 6), (7, 7), (8, 8)]  >>> zip (*test_zip) [( 1, 2, 3, 4), (5, 6, 7, 8)]  >>> 
>>> Zip (a,b,c) [(1, 5, 5), (2, 6, 6), (3, 7, 7), (4, 8, 8)]

Example 2:

>>>Name ('Jack','Beginman','Sony','Pcky')>>>Age (2001, 2003, 2005, 2000)>>> forN,ainchZip (name, age): ...PrintN, a ... jack .2001Beginman2003Sony2005Pcky2000>>>

Look at another example:

>>> all={"Jack": 2001,"beginman": 2003," Sony ": 2005,"pcky": +} for in All.keys ():...      Print  20052001

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)

>>> Z1 = [I/a]>>> z2 = [4,5,6]>>> result = zip (z1,z2)>> > result[(1, 4), (2, 5), (3, 6)]>>> z3 = [4,5,6,7]>>> result = zip (z1, Z3)>>> result[(1, 4), (2, 5), (3, 6)]

The zip () Mate * operator allows you to unzip a list object that has already been zipped:

>>> result[(1, 4), (2, 5), (3, 6)]>>> >>> Zip (*result) [(1, 2, 3), ( 4, 5, 6)]>>> result[(1, 4), (2, 5), (3, 6)]

More on the next level of understanding:
Content Source: http://www.cnblogs.com/diyunpeng/archive/2011/09/15/2177028.html

*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')]

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)]>>> 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'}

The use of the zip () function in Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.