问题:清除清单的不同方法
当您想在Python中清除列表时,有什么理由要做比这两行之一更复杂的事情吗?
old_list = []
old_list = list()
我问的原因是我在一些正在运行的代码中看到了这一点:
del old_list[ 0:len(old_list) ]
Is there any reason to do anything more complicated than one of these two lines when you want to clear a list in Python?
old_list = []
old_list = list()
The reason I ask is that I just saw this in some running code:
del old_list[ 0:len(old_list) ]
回答 0
清除适当的列表将影响同一列表的所有其他引用。
例如,此方法不会影响其他引用:
>>> a = [1, 2, 3]
>>> b = a
>>> a = []
>>> print(a)
[]
>>> print(b)
[1, 2, 3]
但这确实做到了:
>>> a = [1, 2, 3]
>>> b = a
>>> del a[:] # equivalent to del a[0:len(a)]
>>> print(a)
[]
>>> print(b)
[]
>>> a is b
True
您也可以这样做:
>>> a[:] = []
Clearing a list in place will affect all other references of the same list.
For example, this method doesn’t affect other references:
>>> a = [1, 2, 3]
>>> b = a
>>> a = []
>>> print(a)
[]
>>> print(b)
[1, 2, 3]
But this one does:
>>> a = [1, 2, 3]
>>> b = a
>>> del a[:] # equivalent to del a[0:len(a)]
>>> print(a)
[]
>>> print(b)
[]
>>> a is b
True
You could also do:
>>> a[:] = []
回答 1
这样alist = []
做不会清除列表,只会创建一个空列表并将其绑定到变量alist
。如果旧列表具有其他变量绑定,它将仍然存在。
要实际上就地清除列表,可以使用以下任何一种方法:
alist.clear() # Python 3.3+, most obvious
del alist[:]
alist[:] = []
alist *= 0 # fastest
有关更多详细信息,请参见可变序列类型文档页面。
Doing alist = []
does not clear the list, just creates an empty list and binds it to the variable alist
. The old list will still exist if it had other variable bindings.
To actually clear a list in-place, you can use any of these ways:
alist.clear() # Python 3.3+, most obvious
del alist[:]
alist[:] = []
alist *= 0 # fastest
See the Mutable Sequence Types documentation page for more details.
回答 2
有一种清除python列表的非常简单的方法。使用del list_name [:]。
例如:
>>> a = [1, 2, 3]
>>> b = a
>>> del a[:]
>>> print a, b
[] []
There is a very simple way to clear a python list. Use del list_name[:].
For example:
>>> a = [1, 2, 3]
>>> b = a
>>> del a[:]
>>> print a, b
[] []
回答 3
在我看来,del会给您内存,而分配新列表将仅在gc运行时删除旧列表。
这对于大型列表可能很有用,但对于小型列表则应该忽略不计。
编辑:作为Algorias,没关系。
注意
del old_list[ 0:len(old_list) ]
相当于
del old_list[:]
It appears to me that del will give you the memory back, while assigning a new list will make the old one be deleted only when the gc runs.matter.
This may be useful for large lists, but for small list it should be negligible.
Edit: As Algorias, it doesn’t matter.
Note that
del old_list[ 0:len(old_list) ]
is equivalent to
del old_list[:]
回答 4
del list[:]
将删除该列表变量的值
del list
将从存储器中删除变量本身
del list[:]
Will delete the values of that list variable
del list
Will delete the variable itself from memory
回答 5
在两种情况下,您可能需要清除列表:
- 您想
old_list
在代码中进一步使用该名称;
- 您希望尽快对旧列表进行垃圾回收,以释放一些内存。
在第一种情况下,您只需继续进行以下操作:
old_list = [] # or whatever you want it to be equal to
在情况2中,该del
语句将减少对名称所old list
指向的列表对象的引用计数。如果列表对象仅由其名称指向old_list
,则引用计数将为0,并且该对象将被释放以进行垃圾回收。
del old_list
There are two cases in which you might want to clear a list:
- You want to use the name
old_list
further in your code;
- You want the old list to be garbage collected as soon as possible to free some memory;
In case 1 you just go on with the assigment:
old_list = [] # or whatever you want it to be equal to
In case 2 the del
statement would reduce the reference count to the list object the name old list
points at. If the list object is only pointed by the name old_list
at, the reference count would be 0, and the object would be freed for garbage collection.
del old_list
回答 6
如果您要清除列表,显然,您不再需要该列表。如果是这样,您可以通过简单的del方法删除整个列表。
a = [1, 3, 5, 6]
del a # This will entirely delete a(the list).
但是,以防万一,您再次需要它时,可以重新初始化它。或者只是简单地通过清除其元素
del a[:]
If you’re clearing the list, you, obviously, don’t need the list anymore. If so, you can just delete the entire list by simple del method.
a = [1, 3, 5, 6]
del a # This will entirely delete a(the list).
But in case, you need it again, you can reinitialize it. Or just simply clear its elements by
del a[:]
回答 7
另一个可行的解决方案是创建一个空列表作为参考空列表。
empt_list = []
例如,您有一个列表 a_list = [1,2,3]
。要清除它,只需执行以下操作:
a_list = list(empt_list)
a_list
就像一样,这将使一个空列表empt_list
。
another solution that works fine is to create empty list as a reference empty list.
empt_list = []
for example you have a list as a_list = [1,2,3]
. To clear it just make the following:
a_list = list(empt_list)
this will make a_list
an empty list just like the empt_list
.