Python - 移除List中重复项的五种常用方法
时间:2021-08-19 16:06:06
手机看文章
扫描二维码
随时随地手机看文章
[导读]方法1:朴素方法 这种方式是在遍历整个list的基础上,将第一个出现的元素添加在新的列表中。✵示例代码:# Python 3 code to demonstrate # removing duplicated from list # using naive methods ...
方法1:朴素方法
这种方式是在遍历整个list的基础上,将第一个出现的元素添加在新的列表中。✵ 示例代码:
# 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:利用OrderedDict.fromkeys()
这是完成特殊任务中最快的方法,利用 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]
方法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))
→ 输出结果:
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-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))
→ 输出结果:
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]]
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
参考资料
[1]Python – Ways to remove duplicates from list: https://www.geeksforgeeks.org/python-ways-to-remove-duplicates-from-list/[2]Python | Remove duplicates from nested list: https://www.geeksforgeeks.org/python-remove-duplicates-from-nested-list/?ref=rp
公众号留言
§01 阳光的眷顾
# 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 list after removing duplicates : [1, 3, 5, 6]
# 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 list after removing duplicates : [1, 3, 5, 6]
# 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 list after removing duplicates : [1, 3, 5, 6]
# 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 list after removing duplicates : [1, 5, 3, 6]
# 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 list after removing duplicates : [1, 5, 3, 6]
# 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))
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
# 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))
The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
卓大大,请问今年的室内组,会有这样的光吗?
回复:
在一些赛区这种情况是有可能的。§02 高压的恐吓
卓大大,投稿。室外电磁赛道。
§03 走走停停的电磁车
卓老师直角可以像这样连续倒车吗?
回复:
这是允许的。§04 电车拉力赛比赛场地
卓大,单车省赛赛道会是前几天邀请赛那样的吗 ?卓大比赛的坡道会比今天黑龙江邀请赛的坡道大吗?
回复:
自从看到黑龙江省赛的这个室外单车比赛现场,也解决了我的一个大难题。这个赛道足够大,满足“拉力赛”的要求。元素足够丰富。同时又通过了比赛的验证,所以这个场地应该是我们暑期分赛区赛场的原型了。需要感谢哈工大组织的这次黑龙江省赛给出的设计方案。只是坡道稍微矮了点。那我要抓紧时间调了。