A detailed description of the zip () function of the Python development note

Source: Internet
Author: User

Share a Zip () function usage under Python today.

Zip () is an intrinsic function of Python that takes a series of iterated objects as parameters, combining the corresponding elements in the object in order into a tuple, each of which contains elements of the corresponding ordinal position in the original sequence, 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. In the case of all parameters of the same length, zip () is similar to map (), without parameters the ZIP () returns an empty list.

using the zip inversion dictionary

<span style= "Font-family:microsoft Yahei;" >>>> m = {' A ': 1, ' B ': 2, ' C ': 3, ' d ': 4}m.items () [(' A ', ' 1 '), (' C ', 3), (' B ', 2), (' d ', 4)] Zip (M.values (), M.K Eys ()) [(1, ' a '), (3, ' C '), (2, ' B '), (4, ' d ')] mi = dict (Zip (m.values (), M.keys ())) Mi{1: ' A ', 2: ' B ', 3: ' C ', 4: ' d '}< /SPAN>

  

Using the * operator, you can extract the list unzip (into a separate sequence), such as an official document.

Cases:

Help (Zip) to the built-in function zip in module __builtin__:zip (...)  Zip (seq1 [, SEQ2 [...]]), [(Seq1[0], seq2[0] ...), (...)

where Zip ([seq1, ...]) Accepts a series of iterative objects as parameters, packages the corresponding elements in the object into tuples, and then returns a list of those tuples. The length of the returned list is the same as the object with the shortest length in the parameter, if the length of the passed parameter is unequal.

x=[1,2,3] y=[1,2,3] z= (1, 1, 1), (2, 2, 2), (3, 3, 3)

  

x= (1,2,3,4) y=[1,2,3]zip (x, y) #传入参数的长度不等, the length of the returned list is the same as the object with the shortest length in the parameter [(1, 1), (2, 2), (3, 3)]

  

X (1, 2, 3, 4) Zip (x) [(1,), (2,), (3,), (4,)]

  

Zip () []

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

x=[1,2,3] y=[' A ', ' B ', ' C ']z=[4,5,6]xyz=zip (x, Y, z) xyz[(1, ' a ', 4), (2, ' B ', 5), (3, ' C ', 6)]zip (*XYZ) [(1, 2, 3), (' A ', ' B ', ' C '), (4, 5, 6)]

  

X=[5,6,7][X] #[x] Generates a list of lists with only one element x[[5, 6, 7]][x]*3 #[x] * 3 to generate a list of 3 elements, [x, X, X][[5, 6, 7], [5, 6, 7], [5, 6, 7]]x[5 , 6, 7] Zip (*[x]*3) #zip (* [x] * 3) equivalent to ZIP (x, x, X) [(5, 5, 5), (6, 6, 6), (7, 7, 7)]

  

name=[' song ', ' ping ', ' Python '] age=[26,26,27] zip (name,age) [(' Song ', ' + '), (' ping ', ' + '), (' Python ', ')] for n,a in Zip (nam e,age):   ... Print N,a...song 26ping 26python 27

OK above is I personally understand the zip () function usage, today's notes end, there are any better suggestions and comments can go to my site: cylinder template to leave a message, thank you!

A detailed description of the zip () function of the Python development note

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.