久久久久久久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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲国产第一页 | 午夜看看 | 一区二区国产精品 | 神马影院午夜伦理片 | 国产1区2区3区 | av超碰在线 | 欧美一区二区三区在线视频 | 极品新婚夜少妇真紧 | 国产免费高清 | 视频一区二区在线 | 91女人18毛片水多国产 | 伊人av网 | 午夜av在线播放 | 国内精品一区二区三区 | 97精品国产97久久久久久免费 | 天天插天天 | 欧美在线观看一区 | 国产激情久久 | 久久久久久久久久久久久久久久久久久 | 国产精品二区一区二区aⅴ污介绍 | 特级丰满少妇一级aaaa爱毛片 | 天天草夜夜草 | 国产成人高清 | 国产成人99久久亚洲综合精品 | 日韩视频中文字幕 | 亚洲一级黄色 | 色婷婷婷 | 久久黄色录像 | 99热国产在线 | 欧洲一区二区三区 | 成人影 | 国产精品www| 成人在线免费视频观看 | 亚洲一区二区中文字幕 | 99视频在线精品免费观看2 | 亚洲69 | 黄色成年人视频 | 国产精品理论 | 成人午夜在线 | 日日干视频 | 日本理伦片午夜理伦片 |