Python zip function

Source: Internet
Author: User

This function returns a list of tuples, where the i-th tuple contains the i--th element from each of the Argument sequences or Iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there was multiple arguments which is all of the same length, zip () was similar to map () with a in itial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using Zip (*[iter (s)]*n).

zip () in conjunction with the * operator can is used to unzip a list:

Pythonapi said above.

Here's your example:

x=[1,2,3]y=[4,5,6]z=[7,8,9]print  zip (×)print  zip (x, y)  Print zip (x, y, z)

The result is:

[(1,), (2,), (3,)] [(1, 4), (2, 5), (3, 6) [(1, 4, 7), (2, 5, 8), (3, 6, 9)]

It can be seen that the zip is the same dimension of the data placed in a list

w= Zip (x, y, z)for in  w:    print'a:{0},b:{1},c:{2} '. Format (A,B,C)

The result is:

A:1,b:4,c:7A:2,b:5,c:8A:3,b:6,c:9

For is the output of each element in a tuple in the list

Print type (w), type (w[1])

' List ' ' tuple '>

x=[1,2,3]y=[4,5,6]z=[7,8,9]w= zip (x, y, z)print  wPrint Zip (*w)

Results:

[(1, 4, 7), (2, 5, 8), (3, 6, 9)] [(1, 2, 3), (4, 5, 6), (7, 8, 9)]

This is zip (*w) is the function to return to just

Python zip function

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.