📜  在Python中删除特定 List 元素的方法

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

在Python中删除特定 List 元素的方法

List 是一个重要的容器,几乎用于日常编程和 Web 开发的所有代码中。它使用得越多,就越需要掌握它,因此需要了解它的操作。

让我们看看删除特定列表元素的不同方法。

方法 #1:使用remove()
remove()可以执行删除列表元素的任务。它的移除是就地的,不需要额外的空间。但它面临的缺点是它只是从列表中删除了第一个匹配项。所有其他事件都不会被删除,因此只有在列表不包含重复项时才有用。

# Python code to demonstrate
# element removal in list
# using remove() method
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : " 
                            + str(test_list1))
  
# using remove() to remove list element3
test_list1.remove(3)
  
# Printing list after removal
# only first occurrence deleted
print ("The list after element removal is : "
                           + str(test_list1))

输出:

The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 4, 6, 3]


方法#2:使用set.disard()
set.disard()可以执行删除列表元素的任务。它的移除是就地的,不需要额外的空间。首先将列表转换为集合,因此删除了其他重复项并牺牲了列表排序。因此,当我们需要保留顺序或需要保留重复时,这不是一个好主意。

# Python code to demonstrate
# element removal in list
# using discard() method
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : " 
                             + str(test_list2))
  
# using discard() to remove list element 4
test_list2 = set(test_list2)
test_list2.discard(4)
  
test_list2 = list(test_list2)
  
# Printing list after removal
# removes element as distinct initially
print ("The list after element removal is : " 
                           + str(test_list2))

输出 :

The list before element removal is : [1, 4, 5, 4, 5]
The list after element removal is : [1, 5]


方法 #3:使用 Lambda函数+ filter()
Lambda 函数一直是一个有用的实用程序,因此可用于仅在 1 行中执行艰巨的任务。这些也可以执行此特定任务。缺点是它们不是就地的并且需要额外的空间或需要覆盖。它实际上构造了一个新列表,并过滤掉所有需要的元素。它删除了所有出现的元素。

# Python code to demonstrate
# element removal in list
# using filter() + Lambda function
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : "
                            + str(test_list1))
  
# using filter() + Lambda function 
# to remove list element 3
test_list1 = list(filter(lambda x: x != 3, test_list1))
  
# Printing list after removal
print ("The list after element removal is : " 
                           + str(test_list1))

输出:

The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 4, 6]

方法#4:使用列表理解
列表推导是执行与 lambda 函数执行的类似任务的更简单方法。它具有无法就位的相同缺点,并且还需要额外的空间或覆盖。最好不需要filter()来执行它。它删除了所有出现的元素。

# Python code to demonstrate
# element removal in list
# using List Comprehension
  
test_list1 = [1, 3, 4, 6, 3]
test_list2 = [1, 4, 5, 4, 5]
  
# Printing initial list
print ("The list before element removal is : "
                            + str(test_list2))
  
  
# using List Comprehension
# to remove list element 4
test_list2 = [x for x in test_list2 if x != 4]
  
# Printing list after removal
print ("The list after element removal is : " 
                           + str(test_list2))

输出 :

The list before element removal is : [1, 4, 5, 4, 5]
The list after element removal is : [1, 5, 5]

方法 #5:使用 pop()
使用带有列表索引的 pop 方法将元素从列表中弹出

# Python code to demonstrate
# element removal in list
# using pop() method
  
test_list1 = [1, 3, 4, 6, 3]
  
# Printing initial list
print ("The list before element removal is : "
                            + str(test_list1))
  
rmv_element = 4
  
# using pop()
# to remove list element 4
if rmv_element in test_list1:
    test_list1.pop(test_list1.index(rmv_element))
  
# Printing list after removal
print ("The list after element removal is : " 
                           + str(test_list1))
  
# Added by Paras Jain(everythingispossible)

输出 :

The list before element removal is : [1, 3, 4, 6, 3]
The list after element removal is : [1, 3, 6, 3]