📜  Python – 从列表中删除重复项的方法

📅  最后修改于: 2022-05-13 01:54:23.656000             🧑  作者: Mango

Python – 从列表中删除重复项的方法

本文重点介绍从包含可能重复项的列表中获取唯一列表的操作之一。从列表操作中删除重复项有大量的应用程序,因此,拥有它的知识是很好的。

方法一:朴素的方法
在朴素的方法中,我们简单地遍历列表并将元素的第一次出现附加到新列表中,并忽略该特定元素的所有其他出现。

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

输出 :

The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 3, 5, 6]

方法 2:使用列表推导
此方法的工作原理与上述方法类似,但这只是在列表理解的帮助下完成的较长方法的单行简写。

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

输出 :

The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 3, 5, 6]

方法 3:使用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))

输出 :

The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The list after removing duplicates : [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))

输出 :

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()
这是完成特定任务的最快方法。它首先删除重复项并返回一个必须转换为列表的字典。这在字符串的情况下也很有效。

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

输出 :

The original list is : [1, 5, 3, 6, 3, 5, 6, 1]
The list after removing duplicates : [1, 5, 3, 6]