5.2 The del statement
There is a way to remove an item from a list given its index instead of its value: the del statement. This differs from the pop() method which returns a value. The del statement can also be used to remove slices from a
list or clear the entire list (which we did earlier by assignment of an empty list to the slice). For example:
>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]
del can also be used to delete entire variables:
>>> del a
Referencing the name a hereafter is an error (at least until another value is assigned to it). We'll find other uses for del later

本文介绍了Python中del语句的用法,包括删除列表中的特定元素、子序列和清空整个列表。通过实例演示了如何使用del语句进行高效的数据操作。
838

被折叠的 条评论
为什么被折叠?



