卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章20669本站已运行349

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

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

结论

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

卓越飞翔博客
上一篇: C++程序,用于计算数组元素大于其左侧所有元素且至少有K个元素在其右侧的数量
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏