Python中移除List重复项的五种方法

 更新时间:2021年05月19日 09:05:58   作者:卓晴  
本文列些处几种去除在Python 列表中(list)可能存在的重复项,这在很多应用程序中都会遇到的需求,本文介绍几种方法,感兴趣的可以了解一下

 本文列些处几种去除在Python 列表中(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:利用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重复项的五种方法的文章就介绍到这了,更多相关Python 移除List重复项 内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python实现的一只从百度开始不断搜索的小爬虫

    python实现的一只从百度开始不断搜索的小爬虫

    这是我第三天学python了, 想写一个东西纪念一下吧,于是写了一直爬虫,但是不是好的虫,只能讲网页的关键词存到本地, 但是我觉得基本上算是一只小虫了
    2013-08-08
  • python利用正则表达式排除集合中字符的功能示例

    python利用正则表达式排除集合中字符的功能示例

    在正则表达式里,想匹配一些字符中的一个,也就是说给出一个字符的集合,只要出现这个集合里任意的字符,都是成立的,下面这篇文章主要给大家介绍了关于python利用正则表达式排除集合中字符功能的相关资料,需要的朋友可以参考下。
    2017-10-10
  • python try 异常处理(史上最全)

    python try 异常处理(史上最全)

    为了处理异常,我们使用try...except,这篇文章主要介绍了python try 异常处理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-03-03
  • Python入门学习之类的相关知识总结

    Python入门学习之类的相关知识总结

    今天带大家复习python的基础知识,文中对类的相关知识作了非常详细的介绍,对正在学习python的小伙伴们有很好地帮助,需要的朋友可以参考下
    2021-05-05
  • Python实现钉钉/企业微信自动打卡的示例代码

    Python实现钉钉/企业微信自动打卡的示例代码

    这篇文章主要介绍了Python实现钉钉/企业微信自动打卡的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • Python 异步之如何获取当前和正在运行任务详解

    Python 异步之如何获取当前和正在运行任务详解

    这篇文章主要为大家介绍了Python 异步之如何获取当前和正在运行任务详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Python使用pymupdf实现PDF内容搜索并显示功能

    Python使用pymupdf实现PDF内容搜索并显示功能

    在日常工作和学习中,我们可能需要查找和提取PDF文件中的特定内容,本文将介绍如何使用pymupdf实现PDF内容搜索并显示的功能,需要的可以参考下
    2023-08-08
  • Python常见MongoDB数据库操作实例总结

    Python常见MongoDB数据库操作实例总结

    这篇文章主要介绍了Python常见MongoDB数据库操作,结合实例形式详细总结了Python针对MongoDB数据库相关pymongo库安装以及MongoDB数据库的增删改查等相关操作技巧与注意事项,需要的朋友可以参考下
    2018-07-07
  • Python实现k-means算法

    Python实现k-means算法

    这篇文章主要为大家详细介绍了Python实现k-means算法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • 解决python 出现unknown encoding: idna 的问题

    解决python 出现unknown encoding: idna 的问题

    这篇文章主要介绍了解决python出现 unknown encoding: idna 的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03

最新评论