使用 [:] 列出分配
在赋值
lst = range(100)
和
lst[:] = range(100)
之前,lst
变量已分配给列表:
lst = [1, 2, 3]
lst = range(100)
或
lst = [1, 2, 3]
lst[:] = range(100)
What's the difference between
lst = range(100)
and
lst[:] = range(100)
Before that assignment the lst
variable was already assigned to a list:
lst = [1, 2, 3]
lst = range(100)
or
lst = [1, 2, 3]
lst[:] = range(100)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您这样做时,
您将 name
lst
指向一个对象。它不会以任何方式更改用于指向的旧对象lst
,尽管如果没有其他东西指向该对象,其引用计数将降至零,并且它将得到已删除。当你这样做时,
你会迭代
whatever
,创建一个中间元组,并将元组的每个项目分配给已经存在的lst
对象。这意味着如果多个名称指向同一个对象,当您引用任何名称时,您将看到反映的更改,就像您使用append
或extend
或任何其他就地操作。差异的一个例子:
就速度而言,切片分配速度较慢。有关内存使用情况的更多信息,请参阅 Python 切片分配内存使用情况。
When you do
You're pointing the name
lst
at an object. It doesn't change the old objectlst
used to point to in any way, though if nothing else pointed to that object its reference count will drop to zero and it will get deleted.When you do
You're iterating over
whatever
, creating an intermediate tuple, and assigning each item of the tuple to an index in the already existinglst
object. That means if multiple names point to the same object, you will see the change reflected when you reference any of the names, just as if you useappend
orextend
or any of the other in-place operations.An example of the difference:
When it comes to speed, slice assignment is slower. See Python Slice Assignment Memory Usage for more information about its memory usage.
第一个重新定义内置名称
list
以指向某个列表。第二个失败,并显示
TypeError: 'type' object does not support item assignment
。The first one redefines the built-in name
list
to point to some list.The second fails with
TypeError: 'type' object does not support item assignment
.仅当已经存在名为
list
且允许切片分配的对象时,list[:]
才会起作用。另外,您不应该命名变量
list
,因为有一个名为list
的内置变量,它本身就是list
类型。list[:]
will only work if there is already an object namedlist
that allows slice assignment.Also, you shouldn't name variables
list
because there is a built-in namedlist
which is thelist
type itself.list[:]
指定列表内的范围,在本例中,它定义列表的完整范围,即整个列表并更改它们。另一方面,list=range(100)
会擦除list
的原始内容并设置新内容。但请尝试以下操作:
您看,我们通过赋值更改了前两个元素。这意味着,使用此表示法,您可以一次更改列表中的多个元素。
list[:]
specifies a range within the list, in this case it defines the complete range of the list, i.e. the whole list and changes them.list=range(100)
, on the other hand, kind of wipes out the original contents oflist
and sets the new contents.But try the following:
You see, we changed the first two elements with the assignment. This means, using this notation, you can change several elements in the list once.
[:] 对于制作列表的深层副本也很有用。
对 l 的修改反映在 g 中(因为,两者都指向同一个列表,事实上,g 和 l 都只是 python 中的名称),而不是反映在 f 中(因为,它是 l 的副本)
但是,在你的情况下,它没有任何区别。 (不过,我没有资格评论这两种方法的任何内存使用情况。)
编辑
list[:] = range(100) 更新列表
list = range(100) 创建新列表。
@agf:感谢您指出我的错误
[:] is also useful to make a deep copy of the list.
Modification to l is get reflected in g (because, both point to same list, in fact, both g and l are just names in python), not in f(because, it's a copy of l)
But, in your case, It doesn't make any difference. (Though, I'm not eligible to comment on any memory usage of both methods.)
Edit
list[:] = range(100) updates the list
list = range(100) creates new list.
@agf: thanks for pointing my error
不适用于未初始化的变量,因为它正在修改它。
[:]
指定整个列表/元组。won't work on uninitialized variable, as it is modifying it. The
[:]
specifies the whole list/touple.