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

Python 列表迭代器行為和 next(iterator)

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

問題描述

考慮:

>>> lst = iter([1,2,3])
>>> next(lst)
1
>>> next(lst)
2

因此,正如預期的那樣,推進迭代器是通過改變同一個對象來處理的.

So, advancing the iterator is, as expected, handled by mutating that same object.

既然如此,我希望:

a = iter(list(range(10)))
for i in a:
   print(i)
   next(a)

每隔一個元素跳過一次:對 next 的調用應該將迭代器推進一次,然后循環進行的隱式調用應該將它第二次推進 - 第二次調用的結果將是分配給 i.

to skip every second element: the call to next should advance the iterator once, then the implicit call made by the loop should advance it a second time - and the result of this second call would be assigned to i.

它沒有.循環打印列表中的 all 項,而不跳過任何項.

It doesn't. The loop prints all of the items in the list, without skipping any.

我的第一個想法是這可能會發生,因為循環調用 iter 對它傳遞的內容,這可能會給出一個獨立的迭代器 - 情況并非如此,因為我們有 iter(a) 是一個.

My first thought was that this might happen because the loop calls iter on what it is passed, and this might give an independent iterator - this isn't the case, as we have iter(a) is a.

那么,為什么在這種情況下 next 似乎沒有推進迭代器?

So, why does next not appear to advance the iterator in this case?

推薦答案

你看到的是interpreter回顯next()的返回值除了<每次迭代都會打印代碼>i:

What you see is the interpreter echoing back the return value of next() in addition to i being printed each iteration:

>>> a = iter(list(range(10)))
>>> for i in a:
...    print(i)
...    next(a)
... 
0
1
2
3
4
5
6
7
8
9

所以0print(i)的輸出,1next()的返回值,由交互式解釋器等響應.只有 5 次迭代,每次迭代導致 2 行被寫入終端.

So 0 is the output of print(i), 1 the return value from next(), echoed by the interactive interpreter, etc. There are just 5 iterations, each iteration resulting in 2 lines being written to the terminal.

如果您分配 next() 的輸出,事情會按預期工作:

If you assign the output of next() things work as expected:

>>> a = iter(list(range(10)))
>>> for i in a:
...    print(i)
...    _ = next(a)
... 
0
2
4
6
8

或打印額外信息以區分print()輸出與交互式解釋器回顯:

or print extra information to differentiate the print() output from the interactive interpreter echo:

>>> a = iter(list(range(10)))
>>> for i in a:
...    print('Printing: {}'.format(i))
...    next(a)
... 
Printing: 0
1
Printing: 2
3
Printing: 4
5
Printing: 6
7
Printing: 8
9

換句話說,next() 正在按預期工作,但是由于它從迭代器返回下一個值,并由交互式解釋器回顯,因此您會相信循環有自己的迭代器以某種方式復制.

In other words, next() is working as expected, but because it returns the next value from the iterator, echoed by the interactive interpreter, you are led to believe that the loop has its own iterator copy somehow.

這篇關于Python 列表迭代器行為和 next(iterator)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 日本色高清 | 欧美啊v在线观看 | 国产精品久久久久久久免费大片 | 国产日韩欧美综合 | 亚洲国产成人在线观看 | av先锋资源 | 久久久国产精品入口麻豆 | 日韩在线小视频 | 一区二区在线免费观看 | 日本超碰| 中国一级毛片免费 | 日本黄色免费视频 | 毛片久久久 | 国产一区二区成人 | 成人1区2区| 日日夜夜精品视频 | 91福利网址 | 欧美aa在线 | 一本大道久久a久久精二百 国产成人免费在线 | 成人啊啊啊 | 久久av一区 | 成人免费一区二区 | 青青草在线视频免费观看 | 久久久涩| 国产二区av | 免费一区 | 国产成人一区二区三区 | 国产中文视频 | 亚洲一区中文字幕在线观看 | 欧美大片久久久 | 久久免费视频1 | 天天干夜夜操 | 一级欧美一级日韩片免费观看 | 国产精品一区二区三区久久久 | 久久福利电影 | 国产精品99久久久久久www | 在线免费观看成年人视频 | 国产区高清 | 国产精品视频一区二区三区 | www.亚洲 | 91精品久久久久 |