欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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è)列表(也是一個列表)中具有相同元素(但不一定是相同順序)的元素被認為是重復(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)文章

  • 完美解決Pycharm中matplotlib畫圖中文亂碼問題

    完美解決Pycharm中matplotlib畫圖中文亂碼問題

    這篇文章主要介紹了完美解決Pycharm中matplotlib畫圖中文亂碼問題,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01
  • Python getopt模塊處理命令行選項實例

    Python getopt模塊處理命令行選項實例

    這篇文章主要介紹了Python getopt模塊處理命令行選項實例,本文講解相對簡單,需要的朋友可以參考下
    2014-05-05
  • Python中dilb和face_recognition第三方包安裝失敗的解決

    Python中dilb和face_recognition第三方包安裝失敗的解決

    本文主要介紹了Python中dilb和face_recognition第三方包安裝失敗的解決,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • python機器學(xué)習(xí)算法與數(shù)據(jù)降維分析詳解

    python機器學(xué)習(xí)算法與數(shù)據(jù)降維分析詳解

    這篇文章主要為大家介紹了python機器學(xué)習(xí)算法與數(shù)據(jù)降維的分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步
    2021-11-11
  • PyG搭建GCN需要準備的數(shù)據(jù)格式

    PyG搭建GCN需要準備的數(shù)據(jù)格式

    這篇文章主要為大家介紹了PyG搭建GCN前需要準備的PyG數(shù)據(jù)格式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • python調(diào)用攝像頭的示例代碼

    python調(diào)用攝像頭的示例代碼

    這篇文章主要介紹了python調(diào)用攝像頭的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下
    2020-09-09
  • Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器的步驟詳解

    Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器的步驟詳解

    這篇文章主要介紹了Python使用Tkinter實現(xiàn)轉(zhuǎn)盤抽獎器,,本文分場景通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Python中方法鏈的使用方法

    Python中方法鏈的使用方法

    這篇文章主要為大家詳細介紹了Python中方法鏈的使用方法,方法鏈(method chaining)是面向?qū)ο蟮木幊陶Z言中的一種常見語法,對方法鏈感興趣的小伙伴們可以參考一下
    2016-02-02
  • python二進制轉(zhuǎn)換模塊的具體用法

    python二進制轉(zhuǎn)換模塊的具體用法

    在pyton中,通過struct模塊來對二進制進行轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • Python入門之列表用法詳解

    Python入門之列表用法詳解

    列表是元素的集合,存儲在一個變量中。這篇文章主要為大家介紹一下Python中列表的定義與使用,文中的示例代碼講解詳細,需要的可以參考一下
    2022-09-09

最新評論