此 Python 函数用于从现有列表中删除指定的列表项。在本节中,我们将讨论如何使用此方法通过实际示例删除第一个匹配的列表项。List remove 用于删除第一个匹配项的语法是
Name.remove(Item or Value)
Python list remove 函数在整数上的应用
下面的代码将从 numbers 中删除 75、45 和 10 元素。
numbers = [10, 15, 25, 45, 65, 75, 98, 225]
print("Items are : ", numbers)
numbers.remove(75)
print("After Del 75 are : ", numbers)
numbers.remove(45)
print("After Del 45 are : ", numbers)
numbers.remove(10)
print("After Del 10 are : ", numbers)
Items are : [10, 15, 25, 45, 65, 75, 98, 225]
After Del 75 are : [10, 15, 25, 45, 65, 98, 225]
After Del 45 are : [10, 15, 25, 65, 98, 225]
After Del 10 are : [15, 25, 65, 98, 225]
如何删除字符串列表项?
在本例中,我们声明了一个字符串列表。接下来,我们使用该方法删除了 Orange、Kiwi 和 Apple 等项。
- 我们从 fruits 中传递 Orange 项进行删除,并显示剩余的项。
- 从 fruits 中删除 Kiwi 项。接下来,我们消除了名为 Apple 的元素。
提示:请参阅 Python 编程语言中的 List 和 function 文章。
fruits = ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry']
print("Items are : ", fruits)
fruits.remove('Orange')
print("Items after Orange: ", fruits)
fruits.remove('Kiwi')
print("Items after Kiwi : ", fruits)
fruits.remove('Apple')
print("Items after Apple: ", fruits)
Items are : ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry']
Items after Orange: ['Apple', 'Grape', 'Banana', 'Kiwi', 'Cherry']
Items after Kiwi : ['Apple', 'Grape', 'Banana', 'Cherry']
Items after Apple: ['Grape', 'Banana', 'Cherry']
如果我们知道列表中的元素或值,则使用此 Python 函数删除元素。
- 我们正在从 Fruit1 中删除 Grape 项,并显示剩余的项。
- 从 Fruit2 中删除 Orange 项。我们的 Fruits 包含重复元素,即两个 Orange,但此方法仅删除一个项。这是因为此方法仅删除项的第一个实例,而忽略其余项。
- 我们删除了不存在的项。这就是为什么它抛出错误,说 x 不存在。
# Grape Item from Fruit
Fruit1 = ['Apple', 'Orange', 'Grape', 'Banana']
Fruit1.remove('Grape')
print(Fruit1)
print('========')
# Orange Item from Fruits
Fruit2 = ['Apple', 'Orange', 'Grape', 'Banana', 'Orange']
Fruit2.remove('Orange')
print(Fruit2)
print('========')
# Non Existing Grape Item from Fruits1
Fruit3 = ['Apple', 'Orange', 'Banana']
Fruit3.remove('Grape')
print(Fruit3)
['Apple', 'Orange', 'Banana']
========
['Apple', 'Grape', 'Banana', 'Orange']
========
Traceback (most recent call last):
File "/Users/suresh/Desktop/simple.py", line 14, in <module>
.....
.........
ValueError: x not in it
Python remove 函数在混合列表项上的应用
我将在混合元素上使用它。在这里,我们正在从 fruits 中删除 Grape、16 和 Banana 项。在最后一个语句中,我们试图删除不存在的 0。这就是为什么 list remove 函数会抛出错误。
fruitsLi = ['Apple', 2, 'Grape', -12, 'Banana', 16, 'Kiwi', -22]
print("Items are : ", fruitsLi)
fruitsLi.remove('Grape')
print("Items after Deleting Grape: ", fruitsLi)
fruitsLi.remove(16)
print("Items after Deleting 16 : ", fruitsLi)
fruitsLi.remove('Banana')
print("Items after Deleting Banana: ", fruitsLi)
fruitsLi.remove(0)
print("Items after Deleting 0: ", fruitsLi)
Items are : ['Apple', 2, 'Grape', -12, 'Banana', 16, 'Kiwi', -22]
Items after Deleting Grape: ['Apple', 2, -12, 'Banana', 16, 'Kiwi', -22]
Items after Deleting 16 : ['Apple', 2, -12, 'Banana', 'Kiwi', -22]
Items after Deleting Banana: ['Apple', 2, -12, 'Kiwi', -22]
Traceback (most recent call last):
File "/Users/suresh/Desktop/Sample.py", line 14, in <module>
....
............. x not in it
此程序从嵌套列表中删除元素。
numbers = [[10, 22], [1, 3], [15, 25], [200, 45, 65], [75, 98, 225]]
print("Available Items : ", numbers)
numbers.remove([200, 45, 65])
print("Available Items after : ", numbers)
numbers.remove([10, 22])
print("Available Items after : ", numbers)
numbers.remove([15, 25])
print("Available Items after : ", numbers)
