简 介: 本文列些处几种去除在Python 列表中(list)可能存在的重复项,这在很多应用程序中都会遇到的需求,作为程序员最好了解其中的几种方法 以备在用到时能够写出有效的程序。

关键词listPython重复元素

  • Python – Ways to remove duplicates from list
  • Python | Remove duplicates from nested list

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

Python - 移除List中重复项的五种常用方法相关推荐

  1. python去重复排序_Python实现删除排序数组中重复项的两种方法示例

    本文实例讲述了Python实现删除排序数组中重复项的两种方法.分享给大家供大家参考,具体如下: 对于给定的有序数组nums,移除数组中存在的重复数字,确保每个数字只出现一次并返回新数组的长度 注意:不 ...

  2. android 去重 比较两个list_android 去重 比较两个list_Android 去除list集合中重复项的几种方法...

    因为用到list,要去除重复数据,尝试了几种方法.记录于此... 测试数据: List li1 = new List { "", "", "" ...

  3. android 去重 比较两个list_Android 去除list集合中重复项的几种方法

    因为用到list,要去除重复数据,尝试了几种方法.记录于此... 测试数据: List li1 = new List { "", "", "" ...

  4. C# 移除数组中重复项

    方法一: static void Main(string[] args){//看到数组的第一反应应该是排序int[] array = { 2,4,6,2,8,5,8,10};//去掉数组中重复的项// ...

  5. 去除list集合中重复项的几种方法

    因为用到list,要去除重复数据,尝试了几种方法.记录于此... 测试数据: List<string> li1 = new List<string> { "8&quo ...

  6. python去重复记录_Python列表去重复项的N种方法(实例代码)

    说明 Python语言中列表(List)与其他语言的数组(Array)类似,是一种有序的集合数据结构,Python List可支持各种数据类型,长度也可动态调整,与JS中的数组或Java ArrayL ...

  7. permutations python_为什么Python的itertools.permutations包含重复项? (当原始列表重复时)...

    为什么Python的itertools.permutations包含重复项? (当原始列表重复时) 普遍认为,n个不同符号的列表有n! 排列. 但是,当符号不明确时,在math和其他地方最常见的惯例似 ...

  8. 经典算法面试题目-设计算法移除字符串中重复的字符(1.3)

    题目 Design an algorithm and write code to remove the duplicate characters in a string without using a ...

  9. 从Dart列表中删除重复项的2种方法

    本文向您展示了从 Flutter 中的列表中删除重复项的 2 种方法.第一个适用于原始数据类型列表.第二个稍微复杂一些,但适用于map****列表或对象列表. 转换为 Set 然后反转为 List 这 ...

最新文章

  1. 基于Xml 的IOC 容器-分配路径处理策略
  2. Hibernate缓存 - 第一级缓存
  3. mysql 替换 汉字_MySQL替换文字
  4. mysql 查询列表是否关注_点赞功能,用mysql还是redis?
  5. dede列表分页php,织梦用dede:sql实现列表页分页教程
  6. 金融数据分析与挖掘实战1.4.4-1.5.1
  7. MongoDB副本集配置系列六:定位MongoDB慢的原因
  8. React Native知识7-TabBarIOS组件
  9. cisco 的网络地址转换技术(NAT)
  10. Xftp连接Linux 虚拟机
  11. 诺诺打赏源码_2020二开诺诺视频打赏源码/VIP付费看视频带试看 已对接支付+代理...
  12. 如何将heic格式批量转化jpg
  13. xdm,外包能干吗?
  14. 3的16次方用计算机怎么算,excel2016次方公式怎么用
  15. ant 的详细的入门教程
  16. Linq to sql 求和操作
  17. 刷题 Python: 明码
  18. 企业公众号怎么做内容?这四个阶段要做好
  19. 第三代电力电子半导体:SiC MOSFET学习笔记(四)SiC MOSFET传统驱动电路保护
  20. bio linux 创建_Linux IO请求处理流程-bio和request

热门文章

  1. jUnit Test遇到org.apache.ibatis.binding.BindingException
  2. Java基础学习——多线程(线程间通信-生产者消费者代码示例)
  3. 前端开发我为什么选择cordova
  4. 提升代码内外部质量的22条经验
  5. ActionBar通过Tab进行不同的Fragment之间的交换
  6. ADF Jar包循环引用会出问题
  7. 小希的迷宫(并查集)
  8. 使用java+TestNG进行接口回归测试
  9. Redis命令——哈希(Hash)
  10. [摘录]第8章 与非美国人谈判的技巧