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

刪除列表中反向重復項的 Pythonic 方法

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

問題描述

我有一個配對列表:

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

我想刪除所有重復的地方

and I want to remove any duplicates where

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

所以我們最終只有

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

我可以做一個內在的 &外部循環檢查反向對并附加到列表中,如果不是這種情況,但我確信有一種更 Pythonic 的方式來實現相同的結果.

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 函數并使用 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 所示,但請注意,這僅在以下情況下有效您的列表中有不同的元素.因為像 set 一樣,frozenset 總是包含唯一值.因此,您最終會在子列表中獲得唯一值(丟失數據),這可能不是您想要的.

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.

要輸出一個列表,您總是可以使用 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.

這篇關于刪除列表中反向重復項的 Pythonic 方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 国产超碰人人爽人人做人人爱 | 国产一区二区在线视频 | 成人国产精品一级毛片视频毛片 | 亚洲日日操 | 综合国产 | 99re在线视频免费观看 | 久久国产精99精产国高潮 | 日本在线视频一区二区 | 四虎成人免费视频 | 久久av一区 | 天堂色网 | 日日夜夜精品视频 | 成人av一区二区三区 | 中文字幕日韩一区 | 亚洲激情在线观看 | 91电影在线播放 | 久久精品综合 | 人人人人爽 | 看a网站 | 中文字幕第二十页 | 91素人 | 欧美成视频在线观看 | 一级在线毛片 | 日韩在线| 久久久婷| 中文字幕在线观看视频网站 | 香蕉久久a毛片 | 精品国产欧美日韩不卡在线观看 | 久久久久无码国产精品一区 | 国产在线中文字幕 | 国产在线观看免费 | 天天澡天天操 | 亚洲天堂久久新 | 亚洲久久在线 | 伊人操| 夜夜爽99久久国产综合精品女不卡 | 欧美大片一区 | 热久久免费视频 | 一级毛片免费 | 久久久久中文字幕 | 中文字幕在线看第二 |