The powerful zip

Source: Internet
Author: User
Tags python list
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

Output:

Jack 2001

Beginman 2003

Sony 2005

Pcky 2000

Let's look at this section of code:

all={"Jack": 2001, "Beginman": 2003, "Sony": 2005, "Pcky": A-Z} for I in All.keys ():     print I,all[i]

Output:

Sony 2005

Pcky 2000

Jack 2001

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)

>>> type (sorted (s)) 
 
  
   
   >>> type (Zip (s)) 
  
   
    
    >>> type (reversed (s)) 
   
    
     
     >>> type (enumerate (s)) 
    
     
   
    
  
   
 
  

So what is the zip () function?


We help (Zip) to see:

>>> Help (Zip)

Help on built-in function zip in module __builtin__:


Zip (...)

Zip (seq1 [, SEQ2 [...]]), [(Seq1[0], seq2[0] ...), (...)

Return a list of tuples, where each tuple contains the i-th element

From each of the argument sequences. The returned list is truncated

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.

>>> z1=[1,2,3] >>> 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

>>> Zip (*result)

[(1, 2, 3), (4, 5, 6)]

More on the next level of understanding:

* Two-dimensional matrix transformation (matrix of the row and column interchange)

For example, we have a two-dimensional matrix that is described by a list

A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]

With the Python list derivation method, we can also easily complete this task.

print [[Row[col] for row under 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 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, 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.

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


The ③ (*) operator, in conjunction with the ZIP function, enables the ability to reverse the zip and split the merged sequence into multiple tuples.

>>>>x=[1,2,3],y=[' A ', ' B ', ' C ']

>>>>zip (*zip (x, y))

[(a), (' A ', ' B ', ' C ')]

  • Related Article

    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.