Follow the documentation for Python (c): Zip (Python function, in 2. Built-in Functions)

Source: Internet
Author: User
Tags iterable

One, the function of the document:

Zip ():

zip( *iterables)

Make a iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i--th element from each of the argument Sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator. Equivalent to:

def zip (*iterables):    # Zip (' ABCD ', ' xy ')--Ax by    Sentinel = Object ()    iterators = [iter (it) to it in ITER Ables] While    iterators:        result = [] for        it in iterators:            elem = Next (it, Sentinel)            if Elem is Sentin El:                return            result.append (elem)        yield tuple (result)

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) . This repeats, the same iterator times so, each output tuple have the result of calls to the n n Iterato R. This have the effect of dividing the input into n-length chunks.

zip()Should only is used with unequal length inputs if you don ' t care about trailing, unmatched values from the longer iterab Les. If Those values is important, use itertools.zip_longest() instead.

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

>>>

>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = Zip (x, y) >>> list (zipped) [(1, 4), (2, 5 ), (3, 6)]>>> x2, y2 = Zip (*zip (x, y)) >>> x = = List (x2) and y = = List (y2) True

Follow the documentation for Python (c): Zip (Python function, in 2. Built-in Functions)

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.