
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]]
其他子列表中存在的所有子列表均已删除。
删除另一个子列表中已经存在的子列表的问题是用户经常面临的问题,很多时候它会导致消耗用户大量的时间。因此,可以使用上一篇文章中建议的不同方法快速删除另一个子列表中存在的所有子列表。
Atas ialah kandungan terperinci Python - 删除在另一个子列表中存在的子列表. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!
Linux mencari penggunaan arahan
Kumpulan skrip BAT menukar nama fail
tekan sebarang kekunci untuk memulakan semula
Bagaimana untuk mematikan autolengkap yang hebat
Bagaimana untuk memuat turun video dari Douyin
proksi switchysharp
Cara menghidupkan mod selamat Word
Tiga kaedah pengekodan yang biasa digunakan