最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年05月19日 09:05:58   作者:卓晴  
本文列些處幾種去除在Python 列表中(list)可能存在的重復(fù)項(xiàng),這在很多應(yīng)用程序中都會(huì)遇到的需求,本文介紹幾種方法,感興趣的可以了解一下

 本文列些處幾種去除在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 list
test_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 list
test_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()

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

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using set()
  
# initializing list
test_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 ordering
print ("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ǔ)上利用枚舉來(lái)去除重復(fù)元素。通過(guò)檢查元素是否已經(jīng)在列表中存在從而將其略過(guò)。這種方法可以保持列表中的元素順序不會(huì)改變。

示例代碼:

# Python 3 code to demonstrate 
# removing duplicated from list 
# using list comprehension + enumerate()
  
# initializing list
test_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 list
test_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() 方法來(lái)完成任務(wù)。

 示例代碼:

# Python3 code to demonstrate
# removing duplicate sublist 
# using set() + sorted()
  
# initializing list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
                           [1, 2, 3], [3, 4, 1]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using set() + sorted()
# removing duplicate sublist
res = list(set(tuple(sorted(sub)) for sub in test_list))
  
# print result
print("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 list
test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
                           [1, 2, 3], [3, 4, 1]]
  
# printing original list
print("The original list : " + str(test_list))
  
# using set() + map() + sorted()
# removing duplicate sublist
res = list(set(map(lambda i: tuple(sorted(i)), test_list)))
  
# print result
print("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)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python+PyQt5開發(fā)一個(gè)截圖工具

    Python+PyQt5開發(fā)一個(gè)截圖工具

    這篇文章主要為大家詳細(xì)介紹了如何使用Python和PyQt5開發(fā)一個(gè)截圖工具,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-12-12
  • python django中8000端口被占用的解決

    python django中8000端口被占用的解決

    今天小編就為大家分享一篇python django中8000端口被占用的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • Python實(shí)現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能示例

    Python實(shí)現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)定期檢查源目錄與備份目錄的差異并進(jìn)行備份功能,涉及Python基于filecmp模塊的文件比較及讀寫等相關(guān)操作技巧,需要的朋友可以參考下
    2019-02-02
  • 對(duì)python 調(diào)用類屬性的方法詳解

    對(duì)python 調(diào)用類屬性的方法詳解

    今天小編就為大家分享一篇對(duì)python 調(diào)用類屬性的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • Python爬取微信小程序通用方法代碼實(shí)例詳解

    Python爬取微信小程序通用方法代碼實(shí)例詳解

    這篇文章主要介紹了Python爬取微信小程序通用方法代碼實(shí)例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • python中yield的用法詳解

    python中yield的用法詳解

    這篇文章主要介紹了python中yield的用法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • 關(guān)于python并發(fā)編程中的協(xié)程

    關(guān)于python并發(fā)編程中的協(xié)程

    協(xié)程是一種輕量級(jí)的并發(fā)方式,它是在用戶空間中實(shí)現(xiàn)的,并不依賴于操作系統(tǒng)的調(diào)度,協(xié)程可以在同一個(gè)線程中實(shí)現(xiàn)并發(fā),不需要進(jìn)行上下文切換,因此執(zhí)行效率非常高,需要的朋友可以參考下
    2023-04-04
  • Python requests上傳文件實(shí)現(xiàn)步驟

    Python requests上傳文件實(shí)現(xiàn)步驟

    這篇文章主要介紹了Python requests上傳文件實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python內(nèi)置數(shù)據(jù)類型list各方法的性能測(cè)試過(guò)程解析

    Python內(nèi)置數(shù)據(jù)類型list各方法的性能測(cè)試過(guò)程解析

    這篇文章主要介紹了Python內(nèi)置數(shù)據(jù)類型list各方法的性能測(cè)試過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-01-01
  • django-simple-captcha多種驗(yàn)證碼的實(shí)現(xiàn)方法

    django-simple-captcha多種驗(yàn)證碼的實(shí)現(xiàn)方法

    本文介紹了如何在Django項(xiàng)目中配置和使用不同類型的驗(yàn)證碼,包括數(shù)字驗(yàn)證碼、字母驗(yàn)證碼和算術(shù)驗(yàn)證碼,每種驗(yàn)證碼結(jié)合實(shí)例代碼給大家介紹得非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-12-12

最新評(píng)論

临桂县| 开阳县| 双流县| 黑龙江省| 湖北省| 景洪市| 镶黄旗| 内黄县| 凤阳县| 巢湖市| 徐汇区| 衡水市| 张掖市| 娄烦县| 仲巴县| 仁化县| 阳信县| 利辛县| 山丹县| 永修县| 新绛县| 工布江达县| 瑞昌市| 中卫市| 资中县| 慈溪市| 东乡族自治县| 车险| 元朗区| 剑河县| 南召县| 景谷| 栾城县| 惠州市| 康乐县| 郓城县| 岗巴县| 石楼县| 文登市| 普宁市| 隆德县|