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

在迭代列表時從列表中刪除

Removing from a list while iterating over it(在迭代列表時從列表中刪除)
本文介紹了在迭代列表時從列表中刪除的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

以下代碼:

a = list(range(10))
remove = False
for b in a:
    if remove:
        a.remove(b)
    remove = not remove
print(a)

使用 Python 時輸出 [0, 2, 3, 5, 6, 8, 9],而不是 [0, 2, 4, 6, 8]3.2.

Outputs [0, 2, 3, 5, 6, 8, 9], instead of [0, 2, 4, 6, 8] when using Python 3.2.

  1. 為什么會輸出這些特定的值?
  2. 為什么沒有錯誤提示底層迭代器正在被修改?
  3. 在這種行為方面,與早期版本的 Python 相比,機制是否發(fā)生了變化?

請注意,我并不是要解決這種行為,而是要理解它.

Note that I am not looking to work around the behaviour, but to understand it.

推薦答案

我爭論了一段時間來回答這個問題,因為類似的問題在這里已經(jīng)被問過很多次了.但它的獨特性足以讓人們從懷疑中受益.(不過,如果其他人投票結束,我不會反對.)這是對正在發(fā)生的事情的直觀解釋.

I debated answering this for a while, because similar questions have been asked many times here. But it's just unique enough to be given the benefit of the doubt. (Still, I won't object if others vote to close.) Here's a visual explanation of what is happening.

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]       <-  b = 0; remove? no
 ^
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]       <-  b = 1; remove? yes
    ^
[0, 2, 3, 4, 5, 6, 7, 8, 9]          <-  b = 3; remove? no
       ^
[0, 2, 3, 4, 5, 6, 7, 8, 9]          <-  b = 4; remove? yes
          ^
[0, 2, 3, 5, 6, 7, 8, 9]             <-  b = 6; remove? no
             ^
[0, 2, 3, 5, 6, 7, 8, 9]             <-  b = 7; remove? yes
                ^
[0, 2, 3, 5, 6, 8, 9]                <-  b = 9; remove? no
                   ^

由于沒有其他人有,我將嘗試回答您的其他問題:

Since no one else has, I'll attempt to answer your other questions:

為什么沒有給出錯誤指示底層迭代器正在被修改?

Why is no error given to indicate that underlying iterator is being modified?

要在不禁止許多完全有效的循環(huán)構造的情況下拋出錯誤,Python 必須很多了解正在發(fā)生的事情,并且它可能必須在運行時獲取該信息.所有這些信息都需要時間來處理.它會讓 Python 慢很多,只是在速度真正重要的地方——一個循環(huán).

To throw an error without prohibiting many perfectly valid loop constructions, Python would have to know a lot about what's going on, and it would probably have to get that information at runtime. All that information would take time to process. It would make Python a lot slower, in just the place where speed really counts -- a loop.

在這種行為方面,與早期版本的 Python 相比,機制是否發(fā)生了變化?

Have the mechanics changed from earlier versions of Python with respect to this behaviour?

簡而言之,沒有.或者至少我高度對此表示懷疑,而且自從我學習 Python (2.4) 以來它的表現(xiàn)肯定是這樣的.坦率地說,我希望可變序列的任何直接實現(xiàn)都以這種方式運行.哪位知道的好,請指正.(實際上,快速文檔查找確認 從 version 1.4!)

In short, no. Or at least I highly doubt it, and certainly it has behaved this way since I learned Python (2.4). Frankly I would expect any straightforward implementation of a mutable sequence to behave in just this way. Anyone who knows better, please correct me. (Actually, a quick doc lookup confirms that the text that Mikola cited has been in the tutorial since version 1.4!)

這篇關于在迭代列表時從列表中刪除的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
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(根據(jù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 一区在线播放 | 欧美最猛性xxxxx亚洲精品 | 国产成人综合av | 亚洲视频在线免费观看 | 一区二区三区 在线 | aaaa一级毛片 | 一级毛片观看 | 精品永久 | www.9191| 在线观看国产网站 | 中文字幕一区二区不卡 | 国产免费福利小视频 | 噜噜噜噜狠狠狠7777视频 | 亚洲 成人 在线 | 国产欧美一区二区三区另类精品 | 91看片在线观看 | 国产精品色综合 | www.4hu影院 | 国产精品毛片久久久久久久 | 欧美一区二 | 久久国产婷婷国产香蕉 | 亚洲国产精品一区二区三区 | 国产一区二区三区色淫影院 | 日韩欧美在线视频播放 | a级片在线观看 | 亚洲激精日韩激精欧美精品 | 国产精品久久久久不卡 | 欧美精品一区二区三区在线四季 | 成人h动漫亚洲一区二区 | 欧美综合在线视频 | 毛片99| 人人干人人草 | 亚洲国产精品一区二区三区 | 三级成人在线 | 日韩中文字幕 | 久久99久久98精品免观看软件 | 99热.com| 国产精品99久久久久久www | 欧美激情久久久 | 欧美成人a| 精品久久久久香蕉网 |