目录
示例
输出
列表理解
定义函数
比较每个列表
设置操作
结论
首页 后端开发 Python教程 Python - 删除在另一个子列表中存在的子列表

Python - 删除在另一个子列表中存在的子列表

Sep 18, 2023 pm 06:53 PM

Python - 删除在另一个子列表中存在的子列表

Python 是一种广泛使用的软件,它有许多不同的使用目的和执行不同任务的多种功能。 python 的一个有用的功能是列表功能,它有助于收集和存储不同的数据,但很多时候用户在删除另一个子列表中已经存在的子列表时会遇到问题。因此,在本文中,我们将学习如何删除其他子列表中已存在的不同子列表。

为了清楚地理解这个问题,我们举一个例子,我们必须删除数据已经存在于另一个子列表中的子列表。

示例

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
#All the sublist whose data is already present in other sublist are to be removed
登录后复制

输出

名称为 [Shyam,John] 和 [David,Stefan] 的子列表已在其他子列表中具有相同的数据,因此这些额外的子列表将被删除。输出应如下所示:

new_list = [[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
登录后复制

现在我们将了解删除子列表中已存在的子列表的不同方法。

这里我们提到了不同的可能方法:

列表理解

删除其他子列表中存在的所有子列表的最简单方法是借助列表理解。检查列表中存在的所有子列表,并将那些不存在于任何其他子列表中的子列表复制到新列表中。我们举个例子来更清楚地理解:

示例

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
New_list = [sublist for sublist in duplicate_list if not any(set(sublist) <= set(other) for other in duplicate_list if sublist is not other)]
#We first check all the lists of the duplicate list through the any() function and then we check for any repeatation with the help of <= operator
登录后复制

输出

代码完成后,我们将打印上述代码的输出。上述代码的输出如下:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
登录后复制
登录后复制
登录后复制
登录后复制

所有额外的子列表都被删除,因此我们编写了正确的代码来删除子列表中已经存在的子列表。

定义函数

解决该问题的另一种方法是创建一个全新的单独函数来过滤掉其他子列表中存在的所有子列表。这可以通过为函数定义一个条件并使其相应地运行来完成。

示例

def is_sublist(sublist, other):  #is_sublist is the function defined
    return set(sublist) <= set(other)  #the functions checks 2 sublists at a time and if the sublist already exists then it returns with `true` feedback and does not consider the extra sublist

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
new_list = [sublist for sublist in duplicate_list if not any(is_sublist(sublist, other) for other in duplicate_list if sublist is not other)]
登录后复制

输出

上述代码的输出如下:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
登录后复制
登录后复制
登录后复制
登录后复制

所有额外的子列表都被删除,因此我们编写了正确的代码来删除所有额外的子列表。

比较每个列表

这是一种非常复杂的方法,用于删除已存在于另一个子列表中的子列表。在此方法中,所有子列表都会相互比较,并将不重复的子列表复制到新列表中。我们可以借助以下示例来理解这一点:

示例

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]
#A copy of duplicate list is created to avoid any errors in the original file
new_list = duplicate_list[:]

#Check each sublist present in the new_list
for sublist in duplicate_list:
    for other in new_list:
        # Checking of presence of sublist present in other sublist is done
        if sublist != other and set(sublist).issubset(other):   #issubset is used to check presence of one sublist in another sublist
            # Remove all the repeating sublist
            new_list.remove(sublist)
            break  #break is used to stop the loop so that it does not keep checking continuosly

print(new_list)
登录后复制

输出

上述代码的输出如下:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
登录后复制
登录后复制
登录后复制
登录后复制

当列表太长并且包含大量元素较多的子列表时,此方法更合适。

设置操作

在此操作中,在设置操作的帮助下删除其他子列表中存在的子列表。在这种方法中,我们可以将列表中的每个子列表转换为一个集合,并且借助不同的操作,我们可以删除其他子列表中存在的所有子列表。我们通过下面的例子可以更清楚地理解:

示例

duplicate_list = [[Aayush, Shyam, John], [Shyam, John], [Henry, Joe], [David, Stefen, Damon], [David, Stefen]]

new_list = []

for sublist in duplicate_list:
    is_subset = False
    for other in duplicate_list:
        if sublist != other and set(sublist).difference(set(other)) == set():  #The difference operation is used to calculate the difference betwen two sets
            is_subset = True  #When the sublist is present in another sublist the result of is_subset will be true 
            break  #Once the result is found to be true, the loop is broke and all the other sublist are copied into the new_list
    if not is_subset:
        new_list.append(sublist)

print(new_list)
登录后复制

输出

上述代码的输出如下:

[[Aayush, Shyam, John], [Henry, Joe], [David, Stefen, Damon]]
登录后复制
登录后复制
登录后复制
登录后复制

其他子列表中存在的所有子列表均已删除。

结论

删除另一个子列表中已经存在的子列表的问题是用户经常面临的问题,很多时候它会导致消耗用户大量的时间。因此,可以使用上一篇文章中建议的不同方法快速删除另一个子列表中存在的所有子列表。

以上是Python - 删除在另一个子列表中存在的子列表的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌
威尔R.E.P.O.有交叉游戏吗?
1 个月前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何解决Linux终端中查看Python版本时遇到的权限问题? 如何解决Linux终端中查看Python版本时遇到的权限问题? Apr 01, 2025 pm 05:09 PM

Linux终端中查看Python版本时遇到权限问题的解决方法当你在Linux终端中尝试查看Python的版本时,输入python...

在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中? 在Python中如何高效地将一个DataFrame的整列复制到另一个结构不同的DataFrame中? Apr 01, 2025 pm 11:15 PM

在使用Python的pandas库时,如何在两个结构不同的DataFrame之间进行整列复制是一个常见的问题。假设我们有两个Dat...

如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? 如何在10小时内通过项目和问题驱动的方式教计算机小白编程基础? Apr 02, 2025 am 07:18 AM

如何在10小时内教计算机小白编程基础?如果你只有10个小时来教计算机小白一些编程知识,你会选择教些什么�...

如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? 如何在使用 Fiddler Everywhere 进行中间人读取时避免被浏览器检测到? Apr 02, 2025 am 07:15 AM

使用FiddlerEverywhere进行中间人读取时如何避免被检测到当你使用FiddlerEverywhere...

什么是正则表达式? 什么是正则表达式? Mar 20, 2025 pm 06:25 PM

正则表达式是在编程中进行模式匹配和文本操作的强大工具,从而提高了各种应用程序的文本处理效率。

Uvicorn是如何在没有serve_forever()的情况下持续监听HTTP请求的? Uvicorn是如何在没有serve_forever()的情况下持续监听HTTP请求的? Apr 01, 2025 pm 10:51 PM

Uvicorn是如何持续监听HTTP请求的?Uvicorn是一个基于ASGI的轻量级Web服务器,其核心功能之一便是监听HTTP请求并进�...

哪些流行的Python库及其用途? 哪些流行的Python库及其用途? Mar 21, 2025 pm 06:46 PM

本文讨论了诸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和请求等流行的Python库,并详细介绍了它们在科学计算,数据分析,可视化,机器学习,网络开发和H中的用途

Python中如何通过字符串动态创建对象并调用其方法? Python中如何通过字符串动态创建对象并调用其方法? Apr 01, 2025 pm 11:18 PM

在Python中,如何通过字符串动态创建对象并调用其方法?这是一个常见的编程需求,尤其在需要根据配置或运行...

See all articles