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

所有存在于其他子列表中的子列表都被删除了。

结论

删除已经在另一个子列表中存在的子列表的问题是用户经常遇到的问题,并且很多时候会导致用户花费大量时间。因此,可以使用本文中建议的不同方法以快速的方式删除所有存在于另一个子列表中的子列表。

更新于: 2023年8月1日

132 次查看

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告

© . All rights reserved.