Python3中的zip()

-->the startpython

  最近由於寫程序的時候用到過zip()這個內建的方法,網上大多的介紹都是Python2版本的,不多有Python3版本的詳細介紹。對於我這個在Python3下開始學習的菜鳥來講,確實是搞不太懂這其中的區別,因此花了我很長時間纔算基本搞明白zip():app

先來看官方的文檔介紹吧:學習

Python2.7.11中ui

zip([iterable...])lua

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 are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.spa

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).code

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

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

New in version 2.0.blog

Changed in version 2.4: Formerly, zip() required at least one argument and zip() raised a TypeError instead of returning an empty list.ip

Python3.5.1中:

zip(*iterables)

Make an 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) for it in iterables] while iterators: result = [] for it in iterators: elem = next(it, sentinel) if elem is sentinel: 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 n times so that each output tuple has the result of n calls to the iterator. This has the effect of dividing the input into n-length chunks.

zip() should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use itertools.zip_longest() instead.

zip() in conjunction with the * operator can be 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

從標黃的部分能夠看出在Python3中zip()返回的是元祖組成的迭代器,而在Python2中返回的是元祖組成的列表。
從標紅的部分能夠看出在Python3中zip()解包的例子確實是讓人理解不了。。。

我本身寫了一個在Python3下面和在Python2下面的zip()的例子:

這樣也許就能比較直觀的看出zip()的用法吧。。。

再而後,有一天我看到了這麼一行代碼,我又搞不懂了:

如此Pythonic的用法,我這菜鳥真的搞不懂。再加上英文不過關,標綠的部分大概能看懂,還有對迭代器真的不理解。

<-- the end

相關文章
相關標籤/搜索