問題描述
使用 x = [1,2,3,4]
,我可以從 i = iter(x)
獲得一個迭代器.
With x = [1,2,3,4]
, I can get an iterator from i = iter(x)
.
有了這個迭代器,我可以使用 zip 函數創建一個包含兩個項目的元組.
With this iterator, I can use zip function to create a tuple with two items.
>>> i = iter(x)
>>> zip(i,i)
[(1, 2), (3, 4)]
即使我可以使用這種語法來獲得相同的結果.
Even I can use this syntax to get the same results.
>>> zip(*[i] * 2)
[(1, 2), (3, 4)]
這是如何工作的?zip(i,i)
和 zip(*[i] * 2)
的迭代器如何工作?
How does this work? How an iterator with zip(i,i)
and zip(*[i] * 2)
work?
推薦答案
迭代器就像一個項目流.您一次只能查看一個流中的項目,并且您只能訪問第一個元素.要查看流中的某些內容,您需要將其從流中移除,一旦您從流頂部取出某些內容,它就會從流中永久消失.
An iterator is like a stream of items. You can only look at the items in the stream one at a time and you only ever have access to the first element. To look at something in the stream, you need to remove it from the stream and once you take something from the top of the stream, it's gone from the stream for good.
當您調用 zip(i, i)
時,zip
首先查看第一個流并取出一個項目.然后它查看第二個流(恰好與第一個流相同的流)并取出一個項目.然后它從這兩個項目中創建一個元組,并一遍又一遍地重復此操作,直到流中沒有任何內容.
When you call zip(i, i)
, zip
first looks at the first stream and takes an item out. Then it looks at the second stream (which happens to be the same stream as the first one) and takes an item out. Then it makes a tuple out of those two items and repeats this over and over until there is nothing left in the stream.
也許更容易看出我是否要在純 python 中編寫 zip
函數(為簡單起見,只有 2 個參數).它看起來像1:
Maybe it's easier to see if I were to write the zip
function in pure python (with only 2 arguments for simplicity). It would look something like1:
def zip(a, b):
out = []
try:
while True:
item1 = next(a)
item2 = next(b)
out.append((item1, item2))
except StopIteration:
return out
現在想象一下您所討論的情況,其中 a
和 b
是同一個對象.在這種情況下,我們只需在迭代器上調用 next
兩次(在您的示例中為 i
),它將只從 i
中獲取前兩項按順序將它們打包成一個元組.
Now imagine the case that you are talking about where a
and b
are the same object. In that case, we just call next
twice on the iterator (i
in your example case) which will just take the first two items from i
in sequence and pack them into a tuple.
一旦我們了解了 zip(i, i)
的行為方式為什么會這樣,zip(*([i] * 2))
就不一樣了難的.讓我們從內到外閱讀表達式...
Once we've understood why zip(i, i)
behaves the way it does, zip(*([i] * 2))
isn't too hard. Lets read the expression from the inside out...
[i] * 2
這只是創建了一個新列表(長度為 2),其中兩個元素都是對迭代器 i
的引用.所以它和 zip(*[i, i])
是一樣的(當你想重復超過 2 次的時候寫起來更方便).*
unpacking 是 python 中的一個常見習慣用法,您可以在 python 教程.它的要點是 python 獲取可迭代對象并解包"它,就好像可迭代對象的每個項目都是函數的單獨位置參數一樣.所以:
That just creates a new list (of length 2) where both of the elements are references to the iterator i
. So it's the same thing as zip(*[i, i])
(it's just more convenient to write when you want to repeat something many more than 2 times). *
unpacking is a common idiom in python and you can find more information in the python tutorial. The gist of it is that python takes the iterable and "unpacks" it as if each item of the iterable was a separate positional argument to the function. So:
zip(*[i, i])
做同樣的事情:
zip(i, i)
現在鮑勃是我們的叔叔了.我們剛剛開始討論,因為 zip(i, i)
是本次討論的起點.
And now Bob's our uncle. We've just come full-circle since zip(i, i)
is where this discussion started.
1這個示例代碼絕對比前面提到的只接受 2 個參數更簡單.例如,zip
可能會在輸入參數上調用 iter
以便它適用于任何可迭代(不僅僅是迭代器),但這應該足以說明問題跨越...
1This example code is definitely simplified more than just the afore-mentioned only accepting 2 arguments. For example, zip
is probably going to call iter
on the input arguments so that it works for any iterable (not just iterators), but this should be enough to get the point across...
這篇關于Python 迭代器和 zip的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!