关于清除列表的习语的问题 [英] Question about idioms for clearing a list

查看:66
本文介绍了关于清除列表的习语的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道清除清单的标准习语是:


(1)mylist [:] = []

(2)del mylist [:]


我想我不是在切片的心境中,就像有人说的那样,但

可以有人解释一下不同之处在于以下几点:


(3)mylist = []


为什么(1)和(2)首选?我认为前两个是在原地更改

列表,但为什么会更好?最终结果不一样吗?


提前致谢。

-

史蒂文。

I know that the standard idioms for clearing a list are:

(1) mylist[:] = []
(2) del mylist[:]

I guess I''m not in the "slicing frame of mind", as someone put it, but
can someone explain what the difference is between these and:

(3) mylist = []

Why are (1) and (2) preferred? I think the first two are changing the
list in-place, but why is that better? Isn''t the end result the same?

Thanks in advance.
--
Steven.

推荐答案

Steven Watanabe写道:
Steven Watanabe wrote:
我知道清除清单的标准习语是:

(1)mylist [:] = []
(2)del mylist [:]

我想我不是在切片的心境中,正如有人说的那样,但是有人可以解释这些与之间的区别:

(3)mylist = []

为什么(1)和(2)首选?我认为前两个是在原地更改
列表,但为什么会更好?最终结果不一样吗?
I know that the standard idioms for clearing a list are:

(1) mylist[:] = []
(2) del mylist[:]

I guess I''m not in the "slicing frame of mind", as someone put it, but
can someone explain what the difference is between these and:

(3) mylist = []

Why are (1) and (2) preferred? I think the first two are changing the
list in-place, but why is that better? Isn''t the end result the same?




编号考虑这个简单的例子:


class Foo(object) :

def __init __(self,all_my_thingies):

self.all_my_thingies = all_my_thingies

件= [1,2,3,4,5 ]


f = Foo(东西)


东西= []#我被抢了


打印f.all_my_thingies#或不?

原因是l = []只是重新绑定一个新对象(一个列表,但它可能是

任何东西)到一个名字,而l [:] = []将改变对象_referred_到

l。这是一个巨大的差异!

Diez



No. Consider this simple example:

class Foo(object):
def __init__(self, all_my_thingies):
self.all_my_thingies = all_my_thingies
things = [1,2,3,4,5]

f = Foo(things)

things = [] # I''ve been robbed

print f.all_my_thingies # or not?
The reason is that l = [] just rebinds a new object (a list, but it could be
anything) to a name, while l[:] = [] will alter the object _referred_ to by
l. That is a HUGE difference!
Diez


Steven Watanabe写道:
Steven Watanabe wrote:
我知道清除清单的标准习语是:

(1)mylist [:] = []
(2)del mylist [:]

我猜我不像有人说的那样在切片框架中,但有人可以解释这些与它们之间的区别:

(3)mylist = []

为什么(1)和(2)首选?我认为前两个是在原地更改
列表,但为什么会更好?最终结果不一样吗?

提前致谢。
-
史蒂文。
I know that the standard idioms for clearing a list are:

(1) mylist[:] = []
(2) del mylist[:]

I guess I''m not in the "slicing frame of mind", as someone put it, but
can someone explain what the difference is between these and:

(3) mylist = []

Why are (1) and (2) preferred? I think the first two are changing the
list in-place, but why is that better? Isn''t the end result the same?

Thanks in advance.
--
Steven.




解决方案(1)和(2)清除列表的内容(用(1)的空列表替换列表的

内容,或删除
$ b的内容$ b(2)的列表,而解决方案(3)创建一个新列表并将

旧名称绑定到新列表。这意味着你实际上没有修改

(清除)初始列表。


试试吧:



The solution (1) and (2) clear the content of the list (replace the
content of the list by an empty list for (1), or delete the content of
the list for (2) while the solution (3) creates a new list and binds the
old name to the new list. This means that you don''t actually modify
(clear) the initial list.

Just try it out:

a =范围(10)
a
[0,1,2,3,4,5,6,7,8,9] b = a
b
[0,1,2,3,4,5,6,7,8,9] c = a
c
[0,1,2,3,4,5, 6,7,8,9] a.append(10)#断言a,b和c指向同一个列表
a
[0,1,2,3,4,5,6 ,7,8,9,10] b
[0,1,2,3,4,5,6,7,8,9,10] c
[0,1,2,3 ,4,5,6,7,8,9,10] b = []
b
[] a
[0,1 ,2,3,4,5,6,7,8,9,10] del c [:]
c
[] a
[]
a = range(10)
a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] b = a
b [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] c = a
c [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] a.append(10) # assert that a, b and c point to the same list
a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] b [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] c [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] b = []
b [] a [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] del c[:]
c [] a []




这里我们可以看到在b上使用方法(3)并没有修改

a的内容(尽管我们断言它们是相同的列表)。

在c上使用方法(2),另一方面,确实修改了a的内容。


这是因为方法(3)在b上实际创建了一个新列表并绑定了

" b" (作为名称)到该新列表,而不修改列表b。用于

被绑定(这是一个a和c仍然绑定)。



Here we can see that using method (3) on b didn''t modify the content of
a (even though we asserted that they were the same list).
Using method (2) on c, the other hand, did modify the content of a.

This is because method (3) on b actually created a new list and bound
"b" (as a name) to that new list, without modifying the list "b" used to
be bound to (which is the one "a" and "c" are still bound to).


Diez B Roggisch:
Diez B. Roggisch:
原因是l = []只是将一个新对象(一个列表,但它可能是
)重新绑定到一个名称,而l [:] = []将改变对象_referred_由
l。这是一个巨大的差异!
The reason is that l = [] just rebinds a new object (a list, but it could be
anything) to a name, while l[:] = [] will alter the object _referred_ to by
l. That is a HUGE difference!




在我的程序中,我看到版本1和3之间存在另一个实际差异



(1)mylist [:] = []

(3)mylist = []

如果你反复多次创建一个大型mylist ,版本1

更有效地使用内存(可能更少的工作垃圾

收集器),并且程序可以(非常)更快(在某些情况下也是如此)

的实现也与CPython不同。)


再见,

。熊蜂鸟



In my programs I have seen that there is another practical difference
between version 1 and 3:
(1) mylist[:] = []
(3) mylist = []
If you create a big mylist again and again many times, the version 1
uses the memory more efficiently (probably less work for the garbage
collector), and the program can be (quite) faster (this is true in some
implementations different from CPython too).

Bye,
bearophile


这篇关于关于清除列表的习语的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆