📜  Python|从其他列表中存在的列表中删除所有值

📅  最后修改于: 2022-05-13 01:55:34.585000             🧑  作者: Mango

Python|从其他列表中存在的列表中删除所有值

有时我们需要执行从另一个列表中存在的列表中删除所有项目的操作,即我们在一个列表中给出了一些需要从原始列表中删除的无效数字。让我们讨论可以执行此操作的各种方式。
方法 #1:使用列表推导
列表推导式可用于仅在一行中执行简单的方法,因此提供了一种简单的方法来执行此特定任务。

Python3
# Python 3 code to demonstrate
# to remove elements present in other list
# using list comprehension
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using list comprehension to perform task
res = [i for i in test_list if i not in remove_list]
 
# printing result
print ("The list after performing remove operation is : " + str(res))


Python3
# Python 3 code to demonstrate
# to remove elements present in other list
# using filter() + lambda
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using filter() + lambda to perform task
res = filter(lambda i: i not in remove_list, test_list)
 
# printing result
print ("The list after performing remove operation is : " + str(res))


Python3
# Python 3 code to demonstrate
# to remove elements present in other list
# using remove()
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using remove() to perform task
# handled exceptions.
for i in remove_list:
    try:
        test_list.remove(i)
    except ValueError:
        pass
 
# printing result
print ("The list after performing remove operation is : " + str(test_list))


Python3
# Python 3 code to demonstrate
# to remove elements present in other list
# using set()
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using set() to perform task
set1 = set(test_list)
set2 = set(remove_list)
res = list(set1 - set2)
 
# printing result
print ("The list after performing remove operation is : " + str(res))


输出 :

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

方法 #2:使用 filter() + lambda
filter函数可以与 lambda 一起使用来执行此任务并创建一个新的过滤列表,其中包含删除元素列表中不存在的所有元素。

Python3

# Python 3 code to demonstrate
# to remove elements present in other list
# using filter() + lambda
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using filter() + lambda to perform task
res = filter(lambda i: i not in remove_list, test_list)
 
# printing result
print ("The list after performing remove operation is : " + str(res))

输出 :

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

方法 #3:使用 remove()
remove() 也可以执行此任务,但前提是正确处理了未获取特定元素的异常。可以对已删除列表的所有元素进行迭代,并从原始列表中删除这些元素。

Python3

# Python 3 code to demonstrate
# to remove elements present in other list
# using remove()
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using remove() to perform task
# handled exceptions.
for i in remove_list:
    try:
        test_list.remove(i)
    except ValueError:
        pass
 
# printing result
print ("The list after performing remove operation is : " + str(test_list))

输出 :

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]

方法 #4:使用 set()
set() 可用于执行此任务并创建一个新的过滤列表,其中包含删除元素列表中不存在的所有元素。

Python3

# Python 3 code to demonstrate
# to remove elements present in other list
# using set()
 
# initializing list
test_list = [1, 3, 4, 6, 7]
 
# initializing remove list
remove_list = [3, 6]
 
# printing original list
print ("The original list is : " + str(test_list))
 
# printing remove list
print ("The original list is : " + str(remove_list))
 
# using set() to perform task
set1 = set(test_list)
set2 = set(remove_list)
res = list(set1 - set2)
 
# printing result
print ("The list after performing remove operation is : " + str(res))

输出 :

The original list is : [1, 3, 4, 6, 7]
The original list is : [3, 6]
The list after performing remove operation is : [1, 4, 7]