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

遍歷列表并刪除重復元素時出現循環問題

Loop problem while iterating through a list and removing recurring elements(遍歷列表并刪除重復元素時出現循環問題)
本文介紹了遍歷列表并刪除重復元素時出現循環問題的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我想遍歷一個列表,并刪除多次計數的項目,這樣它們就不會被 for 循環重復打印.

I want to iterate through a list, and remove the items that count more than once, so they don't get printed repeatedly by the for loop.

但是,列表中僅出現一次的某些項目似乎也受此影響,我不知道為什么.

However, some items appearing only one time in the list seem to get affected too by this, and I can't figure out why.

任何意見將不勝感激.

示例輸出:

listy = [2,2,1,3,4,2,1,2,3,4,5]
for i in listy:
  if listy.count(i)>1:
    print i, listy.count(i)
    while i in listy: listy.remove(i)
  else:
    print i, listy.count(i)

輸出:

 2 4
 3 2
 1 2

因此完全忽略了 4 和 5.

thus ignoring completely 4 and 5.

推薦答案

您不應該在迭代列表時修改它.這個應該可以工作:

You should not modify a list while iterating over it. This one should work:

listy = [2,2,1,3,4,2,1,2,3,4,5]
found = set()
for i in listy:
    if not i in found:
        print i, listy.count(i)
        found.add(i)

結果是:

2 4
1 2
3 2
4 2
5 1

這篇關于遍歷列表并刪除重復元素時出現循環問題的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产成人精品一区 | 国产精品久久av | 视频第一区| 亚洲午夜精品一区二区三区 | 国产在线观看不卡一区二区三区 | 久久精品国产99国产 | 久久综合狠狠综合久久 | 国产69精品久久久久777 | 九九热精品在线 | 欧美亚洲国产一区二区三区 | 久久久久久国产精品免费免费男同 | 国产午夜视频 | 欧美精品一区二区免费 | 久久久人成影片一区二区三区 | 国产乱码精品1区2区3区 | 欧美1页 | 亚洲精品一区二区三区免 | av网站在线播放 | 日本欧美视频 | 精品亚洲永久免费精品 | 国产在线精品一区二区 | 国产传媒在线观看 | 在线免费观看黄a | 成人黄色a | 91视视频在线观看入口直接观看 | 81精品国产乱码久久久久久 | 国产精品毛片久久久久久久 | 在线视频中文字幕 | 日韩欧美精品一区 | 999久久久久久久 | 国产黄色精品 | 日韩成人高清在线 | 欧美一区二区免费 | 欧美13videosex性极品 | 亚洲欧洲在线视频 | 精品三级| 精精国产xxxx视频在线播放 | 国产69精品久久99不卡免费版 | 国产不卡视频 | 中文字幕高清在线 | 午夜在线影院 |