从Python列表中删除所有出现的元素
任务是执行删除列表中存在的给定项目/元素的所有出现的操作。例子 :
Input : 1 1 2 3 4 5 1 2 1 Output : 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [2, 3, 4, 5, 2] Input : 5 6 7 8 9 10 7 Output : 5 6 8 9 10
在本文中,我们将了解如何以 3 种方式执行此任务:
- 使用列表理解
- 使用 filter() 和 __ne__
- 使用删除()
方法 1:使用列表推导 列表推导可用于执行此任务,我们只需检查匹配项并在没有目标元素的情况下重建列表。我们可以在满足特定条件的列表中创建这些元素的子列表。
Python3
# Python 3 code to demonstrate
# the removal of all occurrences of a
# given item using list comprehension
def remove_items(test_list, item):
# using list comprehension to perform the task
res = [i for i in test_list if i != item]
return res
# driver code
if __name__=="__main__":
# initializing the list
test_list = [1, 3, 4, 6, 5, 1]
# the item which is to be removed
item = 1
# printing the original list
print ("The original list is : " + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print ("The list after performing the remove operation is : " + str(res))
Python3
# Python 3 code to demonstrate
# the removal of all occurrences of
# a given item using filter() and __ne__
def remove_items(test_list, item):
# using filter() + __ne__ to perform the task
res = list(filter((item).__ne__, test_list))
return res
# driver code
if __name__=="__main__":
# initializing the list
test_list = [1, 3, 4, 6, 5, 1]
# the item which is to be removed
item = 1
# printing the original list
print ("The original list is : " + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print ("The list after performing the remove operation is : " + str(res))
Python3
# Python 3 code to demonstrate
# the removal of all occurrences of
# a given item using remove()
def remove_items(test_list, item):
# remove the item for all its occurrences
c = test_list.count(item)
for i in range(c):
test_list.remove(item)
return test_list
# driver code
if __name__=="__main__":
# initializing the list
test_list = [1, 3, 4, 6, 5, 1]
# the item which is to be removed
item = 1
# printing the original list
print ("The original list is :" + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print ("The list after performing the remove operation is :" + str(res))
输出
The original list is : [1, 3, 4, 6, 5, 1]
The list after performing the remove operation is : [3, 4, 6, 5]
方法 2:使用 filter() 和 __ne__ 我们过滤列表中不等于 __ne__ 的项目。
Python3
# Python 3 code to demonstrate
# the removal of all occurrences of
# a given item using filter() and __ne__
def remove_items(test_list, item):
# using filter() + __ne__ to perform the task
res = list(filter((item).__ne__, test_list))
return res
# driver code
if __name__=="__main__":
# initializing the list
test_list = [1, 3, 4, 6, 5, 1]
# the item which is to be removed
item = 1
# printing the original list
print ("The original list is : " + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print ("The list after performing the remove operation is : " + str(res))
输出
The original list is : [1, 3, 4, 6, 5, 1]
The list after performing the remove operation is : [3, 4, 6, 5]
方法3:使用remove() 在这个方法中,我们遍历列表中的每一项,当我们找到要移除的项的匹配项时,我们会调用列表的remove()函数。
Python3
# Python 3 code to demonstrate
# the removal of all occurrences of
# a given item using remove()
def remove_items(test_list, item):
# remove the item for all its occurrences
c = test_list.count(item)
for i in range(c):
test_list.remove(item)
return test_list
# driver code
if __name__=="__main__":
# initializing the list
test_list = [1, 3, 4, 6, 5, 1]
# the item which is to be removed
item = 1
# printing the original list
print ("The original list is :" + str(test_list))
# calling the function remove_items()
res = remove_items(test_list, item)
# printing result
print ("The list after performing the remove operation is :" + str(res))
输出
The original list is : [1, 3, 4, 6, 5, 1]
The list after performing the remove operation is : [3, 4, 6, 5]