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

檢查列表是否有重復(fù)列表

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

問題描述

給定一個(gè)列表列表,我想確保沒有兩個(gè)列表具有相同的值和順序.例如 my_list = [[1, 2, 4, 6, 10], [12, 33, 81, 95, 110], [1, 2, 4, 6, 10]]應(yīng)該返回我是否存在重復(fù)列表,即 [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 但它沒有按我的意愿工作.有人知道如何修復(fù)代碼:

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

推薦答案

你可以計(jì)算列表推導(dǎo)中出現(xiàn)的次數(shù),將它們轉(zhuǎn)換為 tuple 以便你可以散列 &應(yīng)用唯一性:

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)

結(jié)果:

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

足夠簡(jiǎn)單,但由于重復(fù)調(diào)用 count 導(dǎo)致大量循環(huán).還有另一種涉及散列但復(fù)雜度較低的方法是使用 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)

結(jié)果:

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

(只需計(jì)算元組轉(zhuǎn)換的子列表 - 修復(fù)哈希問題 - 并使用列表理解生成 dup 列表,只保留出現(xiàn)多次的項(xiàng)目)

(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)

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

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

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

如果有一些重復(fù),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))

這篇關(guān)于檢查列表是否有重復(fù)列表的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(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è)圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产在线播放一区二区三区 | 国产成人短视频在线观看 | 亚洲精品欧美 | 国产在线观看一区 | www.伊人.com| 亚洲综合二区 | 九九亚洲精品 | 国产精品永久免费视频 | 91婷婷韩国欧美一区二区 | 国产四区| 国产午夜久久 | 午夜三区| 在线观看视频福利 | 欧洲亚洲视频 | 狠狠亚洲| 免费人成激情视频在线观看冫 | 成人天堂噜噜噜 | 国产福利视频 | 国产精品一区二区在线播放 | 欧洲精品久久久久毛片完整版 | 一级毛片观看 | 日韩欧美二区 | 久久久美女 | 成人av网站在线观看 | 91资源在线| 中文字幕在线一 | 精品国产乱码久久久久久丨区2区 | 久久久噜噜噜久久中文字幕色伊伊 | 亚洲国产精品久久久久秋霞不卡 | 亚洲日韩中文字幕一区 | 国产96色在线 | 天啪 | 久草久 | 日韩一区二区三区视频 | 中文字幕不卡在线观看 | 国产免费让你躁在线视频 | 日本免费一区二区三区 | 国产综合精品一区二区三区 | 欧美久久视频 | 午夜一区 | 精品综合久久久 |