鱼C论坛

 找回密码
 立即注册
查看: 2109|回复: 1

[技术交流] Python - 移除List中重复项的五种常用方法

[复制链接]
发表于 2021-5-25 15:58:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 王之叹息 于 2021-5-25 16:01 编辑

方法1:朴素方法
这种方式是在遍历整个list的基础上,将第一个出现的元素添加在新的列表中。

示例代码:
  1. # Python 3 code to demonstrate
  2. # removing duplicated from list
  3. # using naive methods
  4.   
  5. # initializing list
  6. test_list = [1, 3, 5, 6, 3, 5, 6, 1]
  7. print ("The original list is : " +  str(test_list))
  8.   
  9. # using naive method
  10. # to remove duplicated
  11. # from list
  12. res = []
  13. for i in test_list:
  14.     if i not in res:
  15.         res.append(i)
  16.   
  17. # printing list after removal
  18. print ("The list after removing duplicates : " + str(res))
复制代码

→ 输出结果:
  1. The original list is : [1, 3, 5, 6, 3, 5, 6, 1]
  2. The list after removing duplicates : [1, 3, 5, 6]
复制代码

方法2:列表解析
这种方式实际上是第一种方法的简化版,它利用列表解析式,使用一行代码就可以替代上面的循环方式。

示例代码:
  1. # Python 3 code to demonstrate
  2. # removing duplicated from list
  3. # using list comprehension
  4.   
  5. # initializing list
  6. test_list = [1, 3, 5, 6, 3, 5, 6, 1]
  7. print ("The original list is : " +  str(test_list))
  8.   
  9. # using list comprehension
  10. # to remove duplicated
  11. # from list
  12. res = []
  13. [res.append(x) for x in test_list if x not in res]
  14.   
  15. # printing list after removal
  16. 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()
这种方式是最流行的方法来去除列表中的重复元素。但该方法的最大的一个缺点就是使用过后列表中元素的顺序不再继续保持与原来一致了。

示例代码:
  1. # Python 3 code to demonstrate
  2. # removing duplicated from list
  3. # using set()
  4.   
  5. # initializing list
  6. test_list = [1, 5, 3, 6, 3, 5, 6, 1]
  7. print ("The original list is : " +  str(test_list))
  8.   
  9. # using set()
  10. # to remove duplicated
  11. # from list
  12. test_list = list(set(test_list))
  13.   
  14. # printing list after removal
  15. # distorted ordering
  16. 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()
该方法是在列表解析式的基础上利用枚举来去除重复元素。通过检查元素是否已经在列表中存在从而将其略过。这种方法可以保持列表中的元素顺序不会改变。

示例代码:
  1. # Python 3 code to demonstrate
  2. # removing duplicated from list
  3. # using list comprehension + enumerate()
  4.   
  5. # initializing list
  6. test_list = [1, 5, 3, 6, 3, 5, 6, 1]
  7. print ("The original list is : " +  str(test_list))
  8.   
  9. # using list comprehension + enumerate()
  10. # to remove duplicated
  11. # from list
  12. res = [i for n, i in enumerate(test_list) if i not in test_list[:n]]
  13.   
  14. # printing list after removal
  15. 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()。它先是将列表中的重复项移除并返回一个字典,最后转换成列表。这种方法对于字符串也可以进行处理。
示例代码:
  1. # Python 3 code to demonstrate
  2. # removing duplicated from list
  3. # using collections.OrderedDict.fromkeys()
  4. from collections import OrderedDict
  5.   
  6. # initializing list
  7. test_list = [1, 5, 3, 6, 3, 5, 6, 1]
  8. print ("The original list is : " +  str(test_list))
  9.   
  10. # using collections.OrderedDict.fromkeys()
  11. # to remove duplicated
  12. # from list
  13. res = list(OrderedDict.fromkeys(test_list))
  14.   
  15. # printing list after removal
  16. 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()  方法来完成任务。

示例代码:
  1. # Python3 code to demonstrate
  2. # removing duplicate sublist
  3. # using set() + sorted()
  4.   
  5. # initializing list
  6. test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
  7.                            [1, 2, 3], [3, 4, 1]]
  8.   
  9. # printing original list
  10. print("The original list : " + str(test_list))
  11.   
  12. # using set() + sorted()
  13. # removing duplicate sublist
  14. res = list(set(tuple(sorted(sub)) for sub in test_list))
  15.   
  16. # print result
  17. 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()
  1. # Python3 code to demonstrate
  2. # removing duplicate sublist
  3. # using set() + map() + sorted()
  4.   
  5. # initializing list
  6. test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1],
  7.                            [1, 2, 3], [3, 4, 1]]
  8.   
  9. # printing original list
  10. print("The original list : " + str(test_list))
  11.   
  12. # using set() + map() + sorted()
  13. # removing duplicate sublist
  14. res = list(set(map(lambda i: tuple(sorted(i)), test_list)))
  15.   
  16. # print result
  17. 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/py ... plicates-from-list/

[2]
Python | Remove duplicates from nested list: https://www.geeksforgeeks.org/py ... nested-list/?ref=rp
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-9-8 14:24:26 | 显示全部楼层
大佬666向大佬学习,我只会用前两种
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-12 15:06

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表