久久久久久久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)被問過很多次了.但它的獨特性足以讓人們從懷疑中受益.(不過,如果其他人投票結(jié)束,我不會反對.)這是對正在發(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)構(gòu)造的情況下拋出錯誤,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!)

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

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

相關(guā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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久精品99国产精品日本 | 日韩欧美在线观看视频网站 | 欧美男人天堂 | 91精品久久久 | 国产一区二区三区在线看 | 亚洲欧美一区二区三区视频 | 国产三级日本三级 | 91社区在线观看播放 | 国产999精品久久久久久绿帽 | 黄色综合| 国产乱码一二三区精品 | 久久久久久久久久久久91 | 亚洲精品在线视频 | 一级做a毛片 | 最新黄色在线观看 | 色综合久久久 | 精品国产乱码久久久久久丨区2区 | 91在线视频 | 亚洲色图综合网 | 欧美一区二区三区 | a级在线免费视频 | 日韩一区二区三区精品 | 不卡一区二区三区四区 | 国产日韩欧美一区二区 | 欧美一区二区三区在线播放 | www.玖玖玖 | 国产99小视频 | 日本精品一区二区三区在线观看 | 人人精品| 久久久久99 | 国户精品久久久久久久久久久不卡 | 中文一区二区 | 日本涩涩网 | a国产视频 | av在线一区二区三区 | 欧美日韩久 | 国产区高清 | 人人看人人干 | 日韩在线观看 | 中文字幕一区二区三区四区五区 | 亚洲午夜精品一区二区三区他趣 |