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

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]]

所有其他子列表中存在的子列表都被移除。

结论

这个问题是用户经常面临的问题,许多时候它会导致用户花费大量时间。因此,可以使用上述文章中提到的不同方法以快速方式移除所有存在于另一个子列表中的子列表。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程