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

如何在 Python 中使用 for 循環(huán)查找數(shù)組中的重復(fù)元

How to find duplicate elements in array using for loop in Python?(如何在 Python 中使用 for 循環(huán)查找數(shù)組中的重復(fù)元素?)
本文介紹了如何在 Python 中使用 for 循環(huán)查找數(shù)組中的重復(fù)元素?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我有一個(gè)包含重復(fù)元素的列表:

I have a list with duplicate elements:

 list_a=[1,2,3,5,6,7,5,2]

 tmp=[]

 for i in list_a:
     if tmp.__contains__(i):
         print i
     else:
         tmp.append(i)

我已經(jīng)使用上面的代碼在 list_a 中找到了重復(fù)的元素.我不想從列表中刪除元素.

I have used the above code to find the duplicate elements in the list_a. I don't want to remove the elements from list.

但我想在這里使用 for 循環(huán).通常我們使用的 C/C++ 我猜是這樣的:

But I want to use for loop here. Normally C/C++ we use like this I guess:

 for (int i=0;i<=list_a.length;i++)
     for (int j=i+1;j<=list_a.length;j++)
         if (list_a[i]==list_a[j])
             print list_a[i]

我們?nèi)绾卧?Python 中這樣使用?

how do we use like this in Python?

for i in list_a:
    for j in list_a[1:]:
    ....

我嘗試了上面的代碼.但它得到了錯(cuò)誤的解決方案.我不知道如何增加 j 的值.

I tried the above code. But it gets solution wrong. I don't know how to increase the value for j.

推薦答案

僅供參考,在python 2.7+,我們可以使用Counter

Just for information, In python 2.7+, we can use Counter

import collections

x=[1, 2, 3, 5, 6, 7, 5, 2]

>>> x
[1, 2, 3, 5, 6, 7, 5, 2]

>>> y=collections.Counter(x)
>>> y
Counter({2: 2, 5: 2, 1: 1, 3: 1, 6: 1, 7: 1})

唯一列表

>>> list(y)
[1, 2, 3, 5, 6, 7]

找到超過(guò) 1 次的項(xiàng)目

Items found more than 1 time

>>> [i for i in y if y[i]>1]
[2, 5]

僅找到一次的項(xiàng)目

>>> [i for i in y if y[i]==1]
[1, 3, 6, 7]

這篇關(guān)于如何在 Python 中使用 for 循環(huán)查找數(shù)組中的重復(fù)元素?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周?chē)L制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 亚洲精品福利视频 | 久夜精品 | 免费国产一区二区 | 国产伦精品一区二区三区高清 | 国产丝袜一区二区三区免费视频 | 国产精品1区 | 成人精品在线视频 | 国产黄色免费网站 | 久久精品综合网 | 亚洲国产精品一区 | 日韩精品在线看 | 亚洲一区二区三区在线观看免费 | 国产高清在线观看 | 亚洲国产成人av好男人在线观看 | 91久久精品日日躁夜夜躁欧美 | 日本久久福利 | 最新中文字幕在线 | 日韩一区二区三区四区五区 | 韩国av影院 | 亚洲免费精品 | 久久综合色综合 | 国产精品久久久久久亚洲调教 | 毛片一区二区三区 | 国产一区在线看 | 9999久久 | 久久尤物免费一区二区三区 | 国产精品免费在线 | 久久免费精品 | 天天操综合网 | 国产午夜久久久 | 亚洲日本中文字幕在线 | 欧美高清视频 | 欧美伊人影院 | 欧美a∨| 精品免费视频一区二区 | 亚洲国产精品视频一区 | 狠狠爱免费视频 | 国产日韩欧美激情 | 亚洲 中文 欧美 日韩 在线观看 | 精品av天堂毛片久久久借种 | 欧洲一区二区视频 |