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中文网其他相关文章!

2小时内可以学会Python的基本编程概念和技能。1.学习变量和数据类型,2.掌握控制流(条件语句和循环),3.理解函数的定义和使用,4.通过简单示例和代码片段快速上手Python编程。

Python在web开发、数据科学、机器学习、自动化和脚本编写等领域有广泛应用。1)在web开发中,Django和Flask框架简化了开发过程。2)数据科学和机器学习领域,NumPy、Pandas、Scikit-learn和TensorFlow库提供了强大支持。3)自动化和脚本编写方面,Python适用于自动化测试和系统管理等任务。

两小时内可以学到Python的基础知识。1.学习变量和数据类型,2.掌握控制结构如if语句和循环,3.了解函数的定义和使用。这些将帮助你开始编写简单的Python程序。

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

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

Python3.6环境下加载Pickle文件报错:ModuleNotFoundError:Nomodulenamed...

如何解决jieba分词在景区评论分析中的问题?当我们在进行景区评论分析时,往往会使用jieba分词工具来处理文�...

如何使用正则表达式匹配到第一个闭合标签就停止?在处理HTML或其他标记语言时,常常需要使用正则表达式来�...


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver CS6
视觉化网页开发工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具