Python 删除列表中的所有对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14465279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete all objects in a list
提问by Jean-Francois Gallant
I create many object then I store in a list. But I want to delete them after some time because I create news one and don't want my memory goes high (in my case, it jumps to 20 gigs of ram if I don't delete it).
我创建了许多对象,然后存储在一个列表中。但我想在一段时间后删除它们,因为我创建了一个新闻并且不希望我的记忆变高(在我的情况下,如果我不删除它,它会跳到 20 gigs 的内存)。
Here is a little code to illustrate what I trying to do:
这是一个小代码来说明我尝试做的事情:
class test:
def __init__(self):
self.a = "Hello World"
def kill(self):
del self
a = test()
b = test()
c = [a,b]
print("1)Before:",a,b)
for i in c:
del i
for i in c:
i.kill()
print("2)After:",a,b)
A and B are my objects. C is a list of these two objects. I'm trying to delete it definitely with a for-loop in C: one time with DEL and other time with a function. It's not seem to work because the print continue to show the objects.
A 和 B 是我的对象。C 是这两个对象的列表。我正在尝试使用 C 中的 for 循环绝对删除它:一次使用 DEL,另一次使用函数。它似乎不起作用,因为打印继续显示对象。
I need this because I create 100 000 objects many times. The first time I create 100k object, the second time another 100k but I don't need to keep the previous 100k. If I don't delete them, the memory usage goes really high, very quickly.
我需要这个,因为我多次创建 100 000 个对象。我第一次创建 100k 对象,第二次再创建 100k 但我不需要保留之前的 100k。如果我不删除它们,内存使用率会非常高,非常快。
采纳答案by mgilson
tl;dr;
tl;博士;
mylist.clear() # Added in python3.(? maybe 5?)
del mylist[:]
are probably the best ways to do this. The rest of this answer tries to explain why some of your other efforts didn't work.
可能是最好的方法来做到这一点。这个答案的其余部分试图解释为什么你的其他一些努力没有奏效。
cpython at least works on reference counting to determine when objects will be deleted. Here you have multiple references to the same objects. arefers to the same object that c[0]references. When you loop over c(for i in c:), at some point ialso refers to that same object. the delkeyword removes a single reference, so:
cpython 至少适用于引用计数以确定何时删除对象。在这里,您有对相同对象的多个引用。 a指的是c[0]引用的同一个对象。当您循环c( for i in c:) 时,在某些时候i也指向同一个对象。所述del关键字删除单个参考,所以:
for i in c:
del i
creates a reference to an object in cand then deletes that reference -- but the object still has other references (one stored in cfor example) so it will persist.
创建对对象的引用c,然后删除该引用——但该对象仍有其他引用(c例如存储在其中的引用),因此它将持续存在。
In the same way:
以同样的方式:
def kill(self):
del self
only deletes a reference to the object in that method. One way to remove all the references from a list is to use slice assignment:
只删除对该方法中对象的引用。从列表中删除所有引用的一种方法是使用切片赋值:
mylist = list(range(10000))
mylist[:] = []
print(mylist)
Apparently you can also delete the slice to remove objects in place:
显然,您还可以删除切片以删除原位对象:
del mylist[:] #This will implicitly call the `__delslice__` or `__delitem__` method.
This will remove all the references from mylistand also remove the references from anything that refers to mylist. Compared that to simply deleting the list -- e.g.
这将从中删除所有引用,mylist并从引用 的任何内容中删除引用mylist。与简单地删除列表相比 - 例如
mylist = list(range(10000))
b = mylist
del mylist
#here we didn't get all the references to the objects we created ...
print(b) #[0, 1, 2, 3, 4, ...]
Finally, more recent python revisions have added a clearmethod which does the same thing that del mylist[:]does.
最后,最近的 python 修订版添加了一个clear方法,它可以做同样的事情del mylist[:]。
mylist = [1, 2, 3]
mylist.clear()
print(mylist)
回答by Jon-Eric
Here's how you delete every item from a list.
以下是从列表中删除每个项目的方法。
del c[:]
Here's how you delete the first two items from a list.
以下是从列表中删除前两项的方法。
del c[:2]
Here's how you delete a single item from a list (ain your case), assuming cis a list.
这是从列表中删除单个项目的方法(a在您的情况下),假设c是一个列表。
del c[0]
回答by trojek
To delete all objects in a list, you can directly write list = []
删除一个列表中的所有对象,可以直接写 list = []
Here is example:
这是示例:
>>> a = [1, 2, 3]
>>> a
[1, 2, 3]
>>> a = []
>>> a
[]
回答by trojek
If the goal is to delete the objects aand bthemselves (which appears to be the case), forming the list [a, b]is not helpful. Instead, one should keep a list of strings used as the names of those objects. These allow one to delete the objects in a loop, by accessing the globals()dictionary.
如果目标是删除对象a和b它们本身(似乎是这种情况),则形成列表[a, b]没有帮助。相反,应该保留一个字符串列表,用作这些对象的名称。这些允许通过访问globals()字典来删除循环中的对象。
c = ['a', 'b']
# create and work with a and b
for i in c:
del globals()[i]

