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

刪除列表中反向重復(fù)項(xiàng)的 Pythonic 方法

Pythonic way of removing reversed duplicates in list(刪除列表中反向重復(fù)項(xiàng)的 Pythonic 方法)
本文介紹了刪除列表中反向重復(fù)項(xiàng)的 Pythonic 方法的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我有一個(gè)配對列表:

[0, 1], [0, 4], [1, 0], [1, 4], [4, 0], [4, 1]

我想刪除所有重復(fù)的地方

and I want to remove any duplicates where

[a,b] == [b,a]

所以我們最終只有

[0, 1], [0, 4], [1, 4]

我可以做一個(gè)內(nèi)在的 &外部循環(huán)檢查反向?qū)Σ⒏郊拥搅斜碇校绻皇沁@種情況,但我確信有一種更 Pythonic 的方式來實(shí)現(xiàn)相同的結(jié)果.

I can do an inner & outer loop checking for the reverse pair and append to a list if that's not the case, but I'm sure there's a more Pythonic way of achieving the same results.

推薦答案

如果需要保留列表中元素的順序的話,可以使用sorted 函數(shù)并使用 map 像這樣:

If you need to preserve the order of the elements in the list then, you can use a the sorted function and set comprehension with map like this:

lst = [0, 1], [0, 4], [1, 0], [1, 4], [4, 0], [4, 1]
data = {tuple(item) for item in map(sorted, lst)}
# {(0, 1), (0, 4), (1, 4)}

或者干脆沒有像這樣的map:

or simply without map like this:

data = {tuple(sorted(item)) for item in lst}

另一種方法是使用 frozenset,如 here 所示,但請注意,這僅在以下情況下有效您的列表中有不同的元素.因?yàn)橄?set 一樣,frozenset 總是包含唯一值.因此,您最終會在子列表中獲得唯一值(丟失數(shù)據(jù)),這可能不是您想要的.

Another way is to use a frozenset as shown here however note that this only work if you have distinct elements in your list. Because like set, frozenset always contains unique values. So you will end up with unique value in your sublist(lose data) which may not be what you want.

要輸出一個(gè)列表,您總是可以使用 list(map(list, result)),其中 result 是一組元組,僅在 Python-3.0 或更高版本中.

To output a list, you can always use list(map(list, result)) where result is a set of tuple only in Python-3.0 or newer.

這篇關(guān)于刪除列表中反向重復(fù)項(xiàng)的 Pythonic 方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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 檢測和跟蹤人員?)
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ù)文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 黄色网址在线免费观看 | 久久久天堂 | 亚洲成人精品 | 久操久操| 国产在线天堂 | 中文字幕在线播放视频 | 欧美一级黄 | 天天干夜夜操 | 黄色片免费观看 | 九九免费视频 | av老司机在线| av中文在线 | 精品亚洲一区二区三区 | 国产精品毛片一区视频播 | a级一级片 | 欧美日本在线 | 色接久久| 欧美理伦 | 久久精品久久久久 | 国产精品久久久久久久免费看 | 日韩精品国产精品 | 国产激情视频在线 | 国产精品久久久一区二区三区 | 日本少妇视频 | 精品一区二区三区四区 | 69精品视频| 国产草草| 丁香午夜 | 91久久综合亚洲鲁鲁五月天 | av久草 | 日本免费在线视频 | 精品理论片 | 久婷婷 | 中文字幕在线观看网址 | 五月婷婷网站 | 一级片免费在线观看 | 日韩小视频| 国产精品自拍一区 | 国产精品手机在线观看 | 中文字幕高清 | 人体free性hd |