久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Python 迭代器和 zip

Python iterator and zip(Python 迭代器和 zip)
本文介紹了Python 迭代器和 zip的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

使用 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

現在想象一下您所討論的情況,其中 ab 是同一個對象.在這種情況下,我們只需在迭代器上調用 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 成人视屏在线观看 | 久久久久久亚洲精品 | 久久精品视频在线免费观看 | 亚洲成人在线网 | 精品一区二区在线观看 | 成人国产在线视频 | 国产免费a| 欧美久久一级特黄毛片 | 国产视频在线观看一区二区三区 | 毛片久久久 | 日本一区二区三区精品视频 | 天天干精品 | a在线视频 | 国产精品地址 | 国产精品美女www | 老司机成人在线 | 久久国产精品视频 | 成av在线| 自拍偷拍亚洲一区 | 欧美黑人一区 | 中文字幕国 | 99久久精品视频免费 | 夜夜爆操| 欧美日韩在线播放 | 国产一级特黄aaa大片评分 | 请别相信他免费喜剧电影在线观看 | 国产乱码精品1区2区3区 | 国产精品一区二区欧美 | 一区二区三区免费 | 久久久久国产精品免费免费搜索 | 日韩一级免费大片 | 久久精品国产99国产 | 免费性视频 | 亚洲天堂久久新 | 中文字字幕一区二区三区四区五区 | 国产免费视频 | 欧美99 | 欧美日韩国产精品激情在线播放 | 97av视频| 亚洲精品国产成人 | 午夜欧美日韩 |