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

檢查列表是否有重復列表

Checking if a list has duplicate lists(檢查列表是否有重復列表)
本文介紹了檢查列表是否有重復列表的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

給定一個列表列表,我想確保沒有兩個列表具有相同的值和順序.例如 my_list = [[1, 2, 4, 6, 10], [12, 33, 81, 95, 110], [1, 2, 4, 6, 10]]應該返回我是否存在重復列表,即 [1, 2, 4, 6, 10].

Given a list of lists, I want to make sure that there are no two lists that have the same values and order. For instance with my_list = [[1, 2, 4, 6, 10], [12, 33, 81, 95, 110], [1, 2, 4, 6, 10]] it is supposed to return me the existence of duplicate lists, i.e. [1, 2, 4, 6, 10].

我使用了 while 但它沒有按我的意愿工作.有人知道如何修復代碼:

I used while but it doesn't work as I want. Does someone know how to fix the code:

routes = [[1, 2, 4, 6, 10], [1, 3, 8, 9, 10], [1, 2, 4, 6, 10]]
r = len(routes) - 1
i = 0
while r != 0:
    if cmp(routes[i], routes[i + 1]) == 0:
        print "Yes, they are duplicate lists!"
    r -= 1
    i += 1

推薦答案

你可以計算列表推導中出現的次數,將它們轉換為 tuple 以便你可以散列 &應用唯一性:

you could count the occurrences in a list comprehension, converting them to a tuple so you can hash & apply unicity:

routes = [[1, 2, 4, 6, 10], [1, 3, 8, 9, 10], [1, 2, 4, 6, 10]]
dups = {tuple(x) for x in routes if routes.count(x)>1}

print(dups)

結果:

{(1, 2, 4, 6, 10)}

足夠簡單,但由于重復調用 count 導致大量循環.還有另一種涉及散列但復雜度較低的方法是使用 collections.Counter:

Simple enough, but a lot of looping under the hood because of repeated calls to count. There's another way, which involves hashing but has a lower complexity would be to use collections.Counter:

from collections import Counter

routes = [[1, 2, 4, 6, 10], [1, 3, 8, 9, 10], [1, 2, 4, 6, 10]]

c = Counter(map(tuple,routes))
dups = [k for k,v in c.items() if v>1]

print(dups)

結果:

[(1, 2, 4, 6, 10)]

(只需計算元組轉換的子列表 - 修復哈希問題 - 并使用列表理解生成 dup 列表,只保留出現多次的項目)

(Just count the tuple-converted sublists - fixing the hashing issue -, and generate dup list using list comprehension, keeping only items which appear more than once)

現在,如果你只是想檢測有一些重復的列表(不打印它們),你可以

Now, if you just want to detect that there are some duplicate lists (without printing them) you could

  • 將列表列表轉換為元組列表,以便您可以在集合中散列它們
  • 比較列表的長度和集合的長度:

如果有一些重復,len 是不同的:

len is different if there are some duplicates:

routes_tuple = [tuple(x) for x in routes]    
print(len(routes_tuple)!=len(set(routes_tuple)))

或者,能夠在 Python 3 中使用 map 的情況非常少見,因此值得一提:

or, being able to use map in Python 3 is rare enough to be mentionned so:

print(len(set(map(tuple,routes))) != len(routes))

這篇關于檢查列表是否有重復列表的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 久久91精品国产 | 久久久久久久91 | 久久777| 久久久女女女女999久久 | 亚洲精品久久久久久国产精华液 | 手机av在线 | 99久久视频 | 欧美一级欧美三级在线观看 | 国产精品99久久久久久www | 国产成人99久久亚洲综合精品 | 久国产视频 | 激情欧美一区二区三区 | 欧美一级在线观看 | 久久久99精品免费观看 | 久久久精品网站 | 91精品国产综合久久久久久首页 | 中文字幕视频一区 | 免费簧片视频 | 日本中文字幕一区 | 成人福利视频网站 | 91亚洲免费| 天堂av在线影院 | 色香婷婷 | 在线国产一区 | 欧美日一区二区 | 久久精品高清视频 | 欧美一区二区免费 | 91大神xh98xh系列全部 | 国产精品一卡二卡三卡 | 亚洲第一成年免费网站 | 国产乱码精品1区2区3区 | 久久国产日本 | 男人天堂网址 | a国产一区二区免费入口 | 婷婷中文字幕 | 一区二区三区在线电影 | 一区二区视频在线观看 | 毛片一区二区三区 | 国产精品成人在线 | 久草网站| 国产jizz女人多喷水99 |