天天看點

6種在 Python 中從 List 中删除重複項的方法

6種在 Python 中從 List 中删除重複項的方法

來源 | https://medium.com/@cookbug/six-ways-to-remove-duplicates-from-list-in-python-970d998b1384

翻譯 | 楊小愛

Python從清單中删除重複項的方法,在本文中列出了6種方法,這些方法在許多應用程式中都會遇到。作為程式員,我們最好了解它們,以便在需要時編寫有效的程式。

方法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))      

 輸出結果:

原始清單是:[1, 3, 5, 6, 3, 5, 6, 1]

删除重複項後的清單:[1, 3, 5, 6]

方法2:了解清單

這個方法其實是第一種方法的簡化版,它使用了清單推導式,可以用一行代碼代替上面的循環方法。

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

這是從清單中删除重複元素的最流行的方法。但是,這種方法最大的缺點之一是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))      

輸出結果:

原始清單是:[1, 5, 3, 6, 3, 5, 6, 1]

删除重複項後的清單:[1, 3, 5, 6]

方法 4:使用清單了解 + enumerate()

此方法使用枚舉根據清單了解删除重複元素。通過檢查該元素是否已存在于清單中來跳過該元素。此方法保持清單中元素的順序。

示例代碼:

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

這是完成特殊任務的最快方式。它首先删除清單中的重複項并傳回一個字典,最後将其轉換為清單。此方法也可用于字元串,之後清單中元素的順序也發生了變化。

# 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:處理嵌套清單中的重複元素

用于多元清單(清單嵌套)重複元素移除。這裡假設清單(也是一個清單)中具有相同元素(但不一定是相同順序)的元素被認為是重複的。然後使用下面的 set() + sorted() 方法完成任務。

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

輸出結果:

原始清單:[[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))      

輸出結果:

原始清單:[[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]

去重後的清單:[(-1, 0, 1), (1, 3, 4), (1, 2, 3)]

本文完~