亿迅智能制造网
工业4.0先进制造技术信息网站!
首页 | 制造技术 | 制造设备 | 工业物联网 | 工业材料 | 设备保养维修 | 工业编程 |
home  MfgRobots >> 亿迅智能制造网 >  >> Industrial programming >> Python

从 Python LIST 中移除元素 [clear, pop, remove, del]

Python List 数据类型可帮助您以有序的顺序存储不同数据类型的项目。数据写在方括号([])内,值用逗号(,)分隔。

在 Python 中,列表数据类型有许多可用的方法可以帮助您从给定列表中删除元素。方法是 remove(), pop()clear() .

除了列表方法,您还可以使用 del 关键字从列表中删除项目。

在本 Python 教程中,您将学习:

列表示例

my_list = ['Guru', 50, 11.50, 'Siya', 50, ['A', 'B', 'C']]

索引从0开始。在列表中:my_list at

0 th 索引我们有字符串'Guru',

Python remove() 方法

Python removes() 方法是列表中可用的内置方法。它有助于从列表中删除给定的第一个匹配元素。

语法:

list.remove(element)

您要从列表中删除的元素。

返回值

该方法没有返回值。

remove() 方法的使用技巧:

以下是使用 remove() 方法时要记住的要点:

示例:使用 remove() 方法从列表中删除一个元素

这是我拥有的示例列表

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] 

该列表具有日期类型字符串和数字的元素。该列表有重复的元素,如数字 12 和字符串 Riya。

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']
my_list.remove(12) # it will remove the element 12 at the start.
print(my_list)
my_list.remove('Riya') # will remove the first Riya from the list
print(my_list)
my_list.remove(100)  #will throw an error
print(my_list)

输出:

['Siya', 'Tiya', 14, 'Riya', 12, 'Riya']
['Siya', 'Tiya', 14, 12, 'Riya']
Traceback (most recent calllast):
File "display.py", line 9, in <module>
    my_list.remove(100)
ValueError: list.remove(x): x not in the list

Python pop() 方法

pop() 方法根据给定的索引从列表中删除一个元素。

语法

list.pop(index)

index:pop()方法只有一个参数叫index。

返回值:

pop() 方法将根据给定的索引返回删除的元素。最终列表也更新了,不会有元素了。

示例:使用 pop() 方法从列表中删除一个元素

示例中将使用的列表是 my_list =[12, ‘Siya’, ‘Tiya’, 14, ‘Riya’, 12, ‘Riya’]。

让我们尝试使用基于以下内容的 pop() 方法删除元素:

在这里,我们正在删除 Tiya 从列表中。索引从 0 开始,所以 Tiya 的索引 是2。

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']

#By passing index as 2 to remove Tiya
name = my_list.pop(2)
print(name)
print(my_list)

#pop() method without index – returns the last element
item = my_list.pop()
print(item)
print(my_list)

#passing index out of range
item = my_list.pop(15)
print(item)
print(my_list)

输出:

Tiya
[12, 'Siya', 14, 'Riya', 12, 'Riya']
Riya
[12, 'Siya', 14, 'Riya', 12]
Traceback (most recent calllast):
File "display.py", line 14, in <module>
item = my_list.pop(15)
IndexError: popindex out of range

Python clear() 方法

clear() 方法将删除列表中存在的所有元素。

语法:

list.clear()

参数:

无参数。

返回值:

没有返回值。 list() 使用 clear() 方法清空。

示例:使用 clear() 方法从列表中删除所有元素

clear() 方法将清空给定的列表。让我们在下面的例子中看看 clear() 的工作原理:

my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']

#Using clear() method
element = my_list.clear()
print(element)
print(my_list)

输出:

None
[]

使用 del 关键字

要从列表中删除元素,可以使用 del 关键字后跟一个列表。您必须将元素的索引传递给列表。索引从 0 开始。

语法:

del list[index]

您还可以使用 del 从列表中分割一系列元素 关键词。列表中的开始/停止索引可以给 del 关键字,并且落在该范围内的元素将被删除。语法如下:

语法:

del list[start:stop]

这是一个示例,显示使用 del 从列表中删除第一个元素、最后一个元素、多个元素 .

my_list = list(range(15))
print("The Original list is ", my_list)

#To remove the firstelement
del my_list[0]
print("After removing first element", my_list)

#To remove last element
del my_list[-1]
print("After removing last element", my_list)

#To remove element for given index : for example index:5
del my_list[5]
print("After removing element from index:5", my_list)

#To remove last 2 elements from the list
del my_list[-2]
print("After removing last 2 elements", my_list)

#To remove multiple elements
delmy_list[1:5]
print("After removing multiple elements from start:stop index (1:5)", my_list)

#To remove multiple elements
del my_list[4:]
print("To remove elements from index 4 till the end (4:)", my_list)

输出:

The Originallist is  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
After removing first element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
After removing last element [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
After removing element from index:5 [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13]
After removing last 2 elements [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13]
After removing multiple elements from start:stop index (1:5) [1, 7, 8, 9, 10, 11, 13]
To remove elements from index 4 till the end (4:) [1, 7, 8, 9]

如何从列表中删除第一个元素?

您可以使用列表方法,例如 remove()、pop() 从列表中删除第一个元素。对于 remove() 方法,您必须传递要删除的第一个元素并弹出索引,即 0。

您也可以使用 del 关键字从列表中删除第一个元素。

下面的示例显示了使用 remove()、pop() 和 del 从列表中删除第一个元素。

my_list1 = ['A', 'B', 'C', 'D', 'E', 'F']
print("The Originallist is ", my_list1)
#Using remove() to remove first element
my_list1.remove('A')
print("Using remove(), the final list is ", my_list1)

my_list1 = ['A', 'B', 'C', 'D', 'E', 'F']
print("The Originallist is ", my_list1)
#Using pop() to remove the first element
element = my_list1.pop(0)
print("The first element removed from my_list1 is ", element)
print("Using pop(), the final list is ", my_list1)

#Using del to remove the first element
my_list2 = ['A', 'B', 'C', 'D', 'E', 'F']
del my_list2[0]
print("Using del, the final list is ", my_list2)

输出:

The Originallist is  ['A', 'B', 'C', 'D', 'E', 'F']
Using remove(), the final list is  ['B', 'C', 'D', 'E', 'F']
The Originallist is  ['A', 'B', 'C', 'D', 'E', 'F']
The first element removed from my_list1 is  A
Using pop(), the final list is  ['B', 'C', 'D', 'E', 'F']
Using del, the final list is  ['B', 'C', 'D', 'E', 'F']

如何在 Python 中从列表中删除多个元素?

列表方法 remove() 和 pop() 旨在删除单个元素。要删除多个方面,请使用 del 关键字。

从列表 ['A', 'B', 'C', 'D', 'E', 'F'] 中,我们要删除元素 B、C 和 D。下面的示例显示了如何使用 删除 删除元素的关键字。

#Using del to remove the multiple elements from list
my_list2 = ['A', 'B', 'C', 'D', 'E', 'F']
print("Originallist is ", my_list2)
del my_list2[1:4]
print("Using del, the final list is ", my_list2)

输出:

Originallist is  ['A', 'B', 'C', 'D', 'E', 'F']
Using del, the final list is  ['A', 'E', 'F']

如何在 Python 中使用索引从列表中删除元素?

要根据索引删除元素,可以使用列表方法 pop() 。即使使用 del 关键字将帮助您删除给定索引的元素。

#Using del to remove the multiple elements from list
my_list1 = ['A', 'B', 'C', 'D', 'E', 'F']
print("Originallist is ", my_list1)
element = my_list1.pop(2)
print("Element removed for index: 2 is ", element)
print("Using pop, the final list is ", my_list1)


#Using del to remove the multiple elements from list
my_list2 = ['A', 'B', 'C', 'D', 'E', 'F']
print("Originallist is ", my_list2)
del my_list2[2]
print("Using del, the final list is ", my_list2)

输出

Originallist is  ['A', 'B', 'C', 'D', 'E', 'F']
Element removed for index: 2 is  C
Using pop, the final list is  ['A', 'B', 'D', 'E', 'F']
Originallist is  ['A', 'B', 'C', 'D', 'E', 'F']
Using del, the final list is  ['A', 'B', 'D', 'E', 'F']

总结:

在 Python 中,列表数据类型有许多可用的方法,可帮助您从给定列表中删除元素。方法是 remove(), pop()clear()。

列表中可用于删除元素的重要内置方法

方法 说明
删除() 它有助于从列表中删除第一个给定的匹配元素。
pop() pop() 方法根据给定的索引从列表中删除一个元素。
clear() clear() 方法将删除列表中的所有元素。

Python

  1. Python 数据类型
  2. Python 运算符
  3. Python pass 语句
  4. Python 函数参数
  5. Python字典
  6. Python range() 函数:Float、List、For 循环示例
  7. Python 列表理解、追加、排序、长度 [示例]
  8. Python 平均值:如何在 Python 中找到列表的平均值
  9. 带有示例的 Python 列表计数()
  10. Python从列表中删除重复项
  11. Python 列表 index() 与示例
  12. Python - 列表