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

 更新时间:2024年10月25日 08:57:51   作者:web前端开发V  
Python从列表中删除重复项的方法,在本文中列出了6种方法,这些方法在许多应用程序中都会遇到,作为程序员,我们最好了解它们,以便在需要时编写有效的程序,感兴趣的小伙伴跟着小编一起来看看吧

方法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删除重复项内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Python 支持向量机分类器的实现

    Python 支持向量机分类器的实现

    这篇文章主要介绍了Python 支持向量机分类器的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01
  • Python异常原理及异常捕捉实现过程解析

    Python异常原理及异常捕捉实现过程解析

    这篇文章主要介绍了Python异常原理及异常捕捉实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • Python实现将sqlite数据库导出转成Excel(xls)表的方法

    Python实现将sqlite数据库导出转成Excel(xls)表的方法

    这篇文章主要介绍了Python实现将sqlite数据库导出转成Excel(xls)表的方法,结合实例形式分析了Python针对sqlite数据库的连接、读取及使用写操作包(xlwt)生成Excel表的相关实现技巧,需要的朋友可以参考下
    2017-07-07
  • 5分钟教会你用Docker部署一个Python应用

    5分钟教会你用Docker部署一个Python应用

    Docker是一个开源项目,为开发人员和系统管理员提供了一个开放平台,可以将应用程序构建、打包为一个轻量级容器,并在任何地方运行,下面这篇文章主要给大家介绍了关于如何通过5分钟教会你用Docker部署一个Python应用,需要的朋友可以参考下
    2022-06-06
  • Python中的套接字编程是什么?

    Python中的套接字编程是什么?

    不可否认,互联网已成为“存在之魂”,其活动以“连接”或“网络”为特征.使用套接字的最关键的基础之一,使这些网络成为可能.本文涵盖了有关使用Python进行套接字编程的所有领域.套接字可以帮助您建立这些连接,而Python无疑可以简化连接,需要的朋友可以参考下
    2021-06-06
  • Python数据可视化详解

    Python数据可视化详解

    数据可视化是一种将庞杂抽象的数据转化为直观易懂的图形的数据呈现技术,它能帮助我们快速把握数据的分布和规律,更加轻松地理解和探索信息,本文通过代码图片详细介绍了Python数据可视化,感兴趣的小伙伴可以参考阅读
    2023-04-04
  • 使用 prometheus python 库编写自定义指标的方法(完整代码)

    使用 prometheus python 库编写自定义指标的方法(完整代码)

    这篇文章主要介绍了使用 prometheus python 库编写自定义指标的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-06-06
  • Python数据分析之分析千万级淘宝数据

    Python数据分析之分析千万级淘宝数据

    网购已经成为人们生活不可或缺的一部分,本次项目基于淘宝app平台数据,通过相关指标对用户行为进行分析,从而探索用户相关行为模式。感兴趣的可以学习一下
    2022-03-03
  • python pickle 和 shelve模块的用法

    python pickle 和 shelve模块的用法

    pickle和shelve模块都可以把python对象存储到文件中,下面来看看它们的用法吧
    2013-09-09
  • web.py 十分钟创建简易博客实现代码

    web.py 十分钟创建简易博客实现代码

    web.py是一款轻量级的Python web开发框架,简单、高效、学习成本低,特别适合作为python web开发的入门框架
    2016-04-04

最新评论