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

Python從List中刪除重復(fù)項的六種方法

 更新時間:2024年10月25日 08:57:51   作者:web前端開發(fā)V  
Python從列表中刪除重復(fù)項的方法,在本文中列出了6種方法,這些方法在許多應(yīng)用程序中都會遇到,作為程序員,我們最好了解它們,以便在需要時編寫有效的程序,感興趣的小伙伴跟著小編一起來看看吧

方法1:最簡單容易的方法

此方法基于遍歷整個列表,將第一個元素添加到新列表中。

# 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é)果:

原始列表是:[1, 3, 5, 6, 3, 5, 6, 1]

刪除重復(fù)項后的列表:[1, 3, 5, 6]

方法2:理解列表

這個方法其實是第一種方法的簡化版,它使用了列表推導(dǎo)式,可以用一行代碼代替上面的循環(huá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))

方法3:使用 set()

這是從列表中刪除重復(fù)元素的最流行的方法。但是,這種方法最大的缺點之一是set后列表中元素的順序不再和原來一樣。

# 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é)果:

原始列表是:[1, 5, 3, 6, 3, 5, 6, 1]

刪除重復(fù)項后的列表:[1, 3, 5, 6]

方法 4:使用列表理解 + enumerate()

此方法使用枚舉根據(jù)列表理解刪除重復(fù)元素。通過檢查該元素是否已存在于列表中來跳過該元素。此方法保持列表中元素的順序。

示例代碼:

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

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

這是完成特殊任務(wù)的最快方式。它首先刪除列表中的重復(fù)項并返回一個字典,最后將其轉(zhuǎn)換為列表。此方法也可用于字符串,之后列表中元素的順序也發(fā)生了變化。

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

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

用于多維列表(列表嵌套)重復(fù)元素移除。這里假設(shè)列表(也是一個列表)中具有相同元素(但不一定是相同順序)的元素被認(rèn)為是重復(fù)的。然后使用下面的 set() + sorted() 方法完成任務(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é)果:

原始列表:[[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]

去重后的列表:[(-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é)果:

原始列表:[[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]

去重后的列表:[(-1, 0, 1), (1, 3, 4), (1, 2, 3)]

到此這篇關(guān)于Python從List中刪除重復(fù)項的六種方法的文章就介紹到這了,更多相關(guān)Python List刪除重復(fù)項內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python 支持向量機(jī)分類器的實現(xiàn)

    Python 支持向量機(jī)分類器的實現(xiàn)

    這篇文章主要介紹了Python 支持向量機(jī)分類器的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01
  • Python異常原理及異常捕捉實現(xiàn)過程解析

    Python異常原理及異常捕捉實現(xiàn)過程解析

    這篇文章主要介紹了Python異常原理及異常捕捉實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Python實現(xiàn)將sqlite數(shù)據(jù)庫導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法

    Python實現(xiàn)將sqlite數(shù)據(jù)庫導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法

    這篇文章主要介紹了Python實現(xiàn)將sqlite數(shù)據(jù)庫導(dǎo)出轉(zhuǎn)成Excel(xls)表的方法,結(jié)合實例形式分析了Python針對sqlite數(shù)據(jù)庫的連接、讀取及使用寫操作包(xlwt)生成Excel表的相關(guān)實現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • 5分鐘教會你用Docker部署一個Python應(yīng)用

    5分鐘教會你用Docker部署一個Python應(yīng)用

    Docker是一個開源項目,為開發(fā)人員和系統(tǒng)管理員提供了一個開放平臺,可以將應(yīng)用程序構(gòu)建、打包為一個輕量級容器,并在任何地方運行,下面這篇文章主要給大家介紹了關(guān)于如何通過5分鐘教會你用Docker部署一個Python應(yīng)用,需要的朋友可以參考下
    2022-06-06
  • Python中的套接字編程是什么?

    Python中的套接字編程是什么?

    不可否認(rèn),互聯(lián)網(wǎng)已成為“存在之魂”,其活動以“連接”或“網(wǎng)絡(luò)”為特征.使用套接字的最關(guān)鍵的基礎(chǔ)之一,使這些網(wǎng)絡(luò)成為可能.本文涵蓋了有關(guān)使用Python進(jìn)行套接字編程的所有領(lǐng)域.套接字可以幫助您建立這些連接,而Python無疑可以簡化連接,需要的朋友可以參考下
    2021-06-06
  • Python數(shù)據(jù)可視化詳解

    Python數(shù)據(jù)可視化詳解

    數(shù)據(jù)可視化是一種將龐雜抽象的數(shù)據(jù)轉(zhuǎn)化為直觀易懂的圖形的數(shù)據(jù)呈現(xiàn)技術(shù),它能幫助我們快速把握數(shù)據(jù)的分布和規(guī)律,更加輕松地理解和探索信息,本文通過代碼圖片詳細(xì)介紹了Python數(shù)據(jù)可視化,感興趣的小伙伴可以參考閱讀
    2023-04-04
  • 使用 prometheus python 庫編寫自定義指標(biāo)的方法(完整代碼)

    使用 prometheus python 庫編寫自定義指標(biāo)的方法(完整代碼)

    這篇文章主要介紹了使用 prometheus python 庫編寫自定義指標(biāo)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06
  • Python數(shù)據(jù)分析之分析千萬級淘寶數(shù)據(jù)

    Python數(shù)據(jù)分析之分析千萬級淘寶數(shù)據(jù)

    網(wǎng)購已經(jīng)成為人們生活不可或缺的一部分,本次項目基于淘寶app平臺數(shù)據(jù),通過相關(guān)指標(biāo)對用戶行為進(jìn)行分析,從而探索用戶相關(guān)行為模式。感興趣的可以學(xué)習(xí)一下
    2022-03-03
  • python pickle 和 shelve模塊的用法

    python pickle 和 shelve模塊的用法

    pickle和shelve模塊都可以把python對象存儲到文件中,下面來看看它們的用法吧
    2013-09-09
  • web.py 十分鐘創(chuàng)建簡易博客實現(xiàn)代碼

    web.py 十分鐘創(chuàng)建簡易博客實現(xiàn)代碼

    web.py是一款輕量級的Python web開發(fā)框架,簡單、高效、學(xué)習(xí)成本低,特別適合作為python web開發(fā)的入門框架
    2016-04-04

最新評論

翁牛特旗| 冕宁县| 遂溪县| 将乐县| 社会| 长沙市| 平湖市| 汉沽区| 互助| 蓬安县| 广安市| 茌平县| 名山县| 宁海县| 柞水县| 苗栗市| 崇明县| 双柏县| 深圳市| 桑日县| 浮山县| 古浪县| 偃师市| 高密市| 长治市| 革吉县| 吉林市| 肥城市| 台南市| 乌拉特前旗| 泗阳县| 三门峡市| 内丘县| 凌云县| 象山县| 息烽县| 贡山| 保定市| 社会| 南和县| 池州市|