编写一个Python程序,从给定的列表中删除所有重复项。集合不允许重复,因此我们可以将列表转换为集合,然后再将其转换回列表,这样就可以删除列表中的重复项。
dupList = [1, 2, 3, 2, 4, 8, 9, 1, 7, 6, 4, 5]
print("List Items = ", dupList)
uniqSet = set(dupList)
uniqList = list(uniqSet)
print("List Items after removing Duplicates = ", uniqList)
输出。

Python程序:使用for循环从列表中删除重复项
此Python程序允许输入列表大小和项目。for循环将遍历dupList中的项目。if语句结合not in运算符检查该值是否不存在于uniqList中。如果为True,则将该值追加到uniqList。
dupList = []
listNumber = int(input("Enter the Total List Items = "))
for i in range(1, listNumber + 1):
listValue = int(input("Enter the %d List Item = " %i))
dupList.append(listValue)
print("List Items = ", dupList)
uniqList = []
for val in dupList:
if val not in uniqList:
uniqList.append(val)
print("List Items after removing Duplicates = ", uniqList)

使用列表推导式
在此示例中,我们使用列表推导式来删除列表中的重复项。此代码与上面的示例相同,但我们使用了列表推导式的概念。
dupList = [1, 2, 5, 8, 1, 9, 11, 5, 22, 6, 2, 8, 14]
print("List Items = ", dupList)
uniqList = []
[uniqList.append(i) for i in dupList if i not in uniqList]
print("List Items after removing Duplicates = ", uniqList)
List Items = [1, 2, 5, 8, 1, 9, 11, 5, 22, 6, 2, 8, 14]
List Items after removing Duplicates = [1, 2, 5, 8, 9, 11, 22, 6, 14]
Python程序:使用collections.fromkeys删除列表重复项
在此示例中,我们从collections导入了OrderedDict,并使用fromkeys函数删除重复项。别忘了将结果转换为列表。
from collections import OrderedDict
dupList = [8, 1, 9, 2, 8, 4, 9, 11, 5, 22, 6, 4, 8]
print("List Items = ", dupList)
uniqList = OrderedDict.fromkeys(dupList)
print("List Items after removing Duplicates = ", list(uniqList))
使用OrderedDict from collections的输出。

使用unique函数删除列表重复项
numpy和pandas模块都有unique函数来删除重复项,因此我们使用了它们,并将结果转换为列表。为了转换结果,我们使用了tolist()函数。
import numpy as np
import pandas as pd
dupList = [1, 2, 2, 4, 1, 5, 6, 8, 6, 8, 9, 7, 4]
print("List Items = ", dupList)
uniqList = np.unique(dupList).tolist()
print("List Items after removing Duplicates = ", uniqList)
uniqList2 = pd.unique(dupList).tolist()
print("List Items after removing Duplicates = ", uniqList2)
numpy unique函数输出
List Items = [1, 2, 2, 4, 1, 5, 6, 8, 6, 8, 9, 7, 4]
List Items after removing Duplicates = [1, 2, 4, 5, 6, 7, 8, 9]
List Items after removing Duplicates = [1, 2, 4, 5, 6, 8, 9, 7]
使用enumerate函数
此示例使用enumerate函数从列表中删除重复项。
from collections import OrderedDict
dupList = [1, 2, 3, 2, 4, 1, 5, 6, 5, 8, 7, 9, 8]
print("List Items = ", dupList)
uniqList = [val for x, val in enumerate(dupList) if val not in dupList[:x]]
print("List Items after removing Duplicates = ", uniqList)
enumerate示例的输出如下所示。
List Items = [1, 2, 3, 2, 4, 1, 5, 6, 5, 8, 7, 9, 8]
List Items after removing Duplicates = [1, 2, 3, 4, 5, 6, 8, 7, 9]