av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術(shù)文章
文章詳情頁

Python中移除List重復(fù)項(xiàng)的五種方法

瀏覽:19日期:2022-06-19 11:41:09

本文列些處幾種去除在Python 列表中(list)可能存在的重復(fù)項(xiàng),這在很多應(yīng)用程序中都會(huì)遇到的需求,作為程序員最好了解其中的幾種方法 以備在用到時(shí)能夠?qū)懗鲇行У某绦颉?/p>方法1:樸素方法

這種方式是在遍歷整個(gè)list的基礎(chǔ)上,將第一個(gè)出現(xiàn)的元素添加在新的列表中。

示例代碼:

# Python 3 code to demonstrate # removing duplicated from list # using naive methods # initializing listtest_list = [1, 3, 5, 6, 3, 5, 6, 1]print ('The original list is : ' + str(test_list)) # using naive method# to remove duplicated # from list res = []for i in test_list: if i not in res:res.append(i) # printing list after removal print ('The list after removing duplicates : ' + str(res))

→ 輸出結(jié)果:The original list is : [1, 3, 5, 6, 3, 5, 6, 1]The list after removing duplicates : [1, 3, 5, 6]

方法2:列表解析式

這種方式實(shí)際上是第一種方法的簡(jiǎn)化版,它利用列表解析式,使用一行代碼就可以替代上面的循環(huán)方式。

示例代碼:

# Python 3 code to demonstrate # removing duplicated from list # using list comprehension # initializing listtest_list = [1, 3, 5, 6, 3, 5, 6, 1]print ('The original list is : ' + str(test_list)) # using list comprehension# to remove duplicated # from list res = [][res.append(x) for x in test_list if x not in res] # printing list after removal print ('The list after removing duplicates : ' + str(res))

→ 輸出結(jié)果:The original list is : [1, 3, 5, 6, 3, 5, 6, 1]The list after removing duplicates : [1, 3, 5, 6]

方法3:使用set()

這種方式是最流行的方法來去除列表中的重復(fù)元素。但該方法的最大的一個(gè)缺點(diǎn)就是使用過后列表中元素的順序不再繼續(xù)保持與原來一致了。

示例代碼:

# Python 3 code to demonstrate # removing duplicated from list # using set() # initializing listtest_list = [1, 5, 3, 6, 3, 5, 6, 1]print ('The original list is : ' + str(test_list)) # using set()# to remove duplicated # from list test_list = list(set(test_list)) # printing list after removal # distorted orderingprint ('The list after removing duplicates : ' + str(test_list))

→ 輸出結(jié)果:The original list is : [1, 5, 3, 6, 3, 5, 6, 1]The list after removing duplicates : [1, 3, 5, 6]

方法4:利用列表解析式 + enumerate()

該方法是在列表解析式的基礎(chǔ)上利用枚舉來去除重復(fù)元素。通過檢查元素是否已經(jīng)在列表中存在從而將其略過。這種方法可以保持列表中的元素順序不會(huì)改變。

示例代碼:

# Python 3 code to demonstrate # removing duplicated from list # using list comprehension + enumerate() # initializing listtest_list = [1, 5, 3, 6, 3, 5, 6, 1]print ('The original list is : ' + str(test_list)) # using list comprehension + enumerate()# to remove duplicated # from list res = [i for n, i in enumerate(test_list) if i not in test_list[:n]] # printing list after removal print ('The list after removing duplicates : ' + str(res))

→ 輸出結(jié)果:The original list is : [1, 5, 3, 6, 3, 5, 6, 1]The list after removing duplicates : [1, 5, 3, 6]

方法5:利用collections.OrderedDict.fromkeys()

這是完成特殊任務(wù)中最快的方法。它先是將列表中的重復(fù)項(xiàng)移除并返回一個(gè)字典,最后轉(zhuǎn)換成列表。這種方法對(duì)于字符串也可以進(jìn)行處理。

示例代碼:

# Python 3 code to demonstrate # removing duplicated from list # using collections.OrderedDict.fromkeys()from collections import OrderedDict # initializing listtest_list = [1, 5, 3, 6, 3, 5, 6, 1]print ('The original list is : ' + str(test_list)) # using collections.OrderedDict.fromkeys()# to remove duplicated # from list res = list(OrderedDict.fromkeys(test_list)) # printing list after removal print ('The list after removing duplicates : ' + str(res))

→ 輸出結(jié)果:The original list is : [1, 5, 3, 6, 3, 5, 6, 1]The list after removing duplicates : [1, 5, 3, 6]

方法6:處理嵌套列表中的重復(fù)元素

對(duì)于多維列表(列表嵌套)中的重復(fù)元素去除。這里假設(shè)列表中元素(也是列表)它們具有相同的元素(但不一定順序相同)都被當(dāng)做重復(fù)元素。那么下面使用 set() + sorted() 方法來完成任務(wù)。

示例代碼:

# Python3 code to demonstrate# removing duplicate sublist # using set() + sorted() # initializing listtest_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] # printing original listprint('The original list : ' + str(test_list)) # using set() + sorted()# removing duplicate sublistres = list(set(tuple(sorted(sub)) for sub in test_list)) # print resultprint('The list after duplicate removal : ' + str(res))

→ 輸出結(jié)果:The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]

也可以利用 set() + map() + sorted()

示例代碼:

# Python3 code to demonstrate# removing duplicate sublist # using set() + map() + sorted() # initializing listtest_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] # printing original listprint('The original list : ' + str(test_list)) # using set() + map() + sorted()# removing duplicate sublistres = list(set(map(lambda i: tuple(sorted(i)), test_list))) # print resultprint('The list after duplicate removal : ' + str(res))

→ 輸出結(jié)果:The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]

到此這篇關(guān)于Python中移除List重復(fù)項(xiàng)的五種方法的文章就介紹到這了,更多相關(guān)Python 移除List重復(fù)項(xiàng) 內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: av影音资源 | 在线日韩 | 91一区二区 | 中国黄色毛片视频 | 亚洲精品一区二区 | 91亚洲免费 | 黑人精品欧美一区二区蜜桃 | 精品一二区 | 一级欧美一级日韩片 | 久久久久久高潮国产精品视 | 999精品网 | 56pao在线| 成人免费视频网站在线观看 | 成人网在线观看 | av在线三级 | 天天干天天草 | 久久人人网 | 日韩精品a在线观看图片 | 日韩免费在线观看视频 | 一区二区三区视频在线观看 | h片在线免费看 | 亚洲精品一区二区在线观看 | 久久福利电影 | 四虎成人精品永久免费av九九 | 超级乱淫av片免费播放 | 成人亚洲性情网站www在线观看 | 精品视频在线观看 | 亚洲精品中文字幕 | 日韩成人一区 | 亚洲黄色片免费观看 | 国产亚洲精品成人av久久ww | 亚洲不卡在线观看 | 男女又爽又黄视频 | 在线欧美小视频 | 亚洲三级免费看 | 日本一区二区电影 | 一级毛片在线播放 | 91资源在线观看 | 欧美精品一区二区三区四区五区 | 曰韩三级 | 国内精品久久久久久久 |