当前位置: 代码网 > it编程>前端脚本>Python > Python从List中删除重复项的六种方法

Python从List中删除重复项的六种方法

2024年10月25日 Python 我要评论
方法1:最简单容易的方法此方法基于遍历整个列表,将第一个元素添加到新列表中。# python 3 code to demonstrate # removing duplicated from list

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

到此这篇关于python从list中删除重复项的六种方法的文章就介绍到这了,更多相关python list删除重复项内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com