a[:] = b 和 a = b[:] 之间的区别? (Python)

发布于 2024-11-29 02:02:12 字数 34 浏览 2 评论 0原文

我被要求进行编码测试,但不知道答案。有人有什么想法吗?

I was asked this for a coding test and didn't know the answer. Anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清眉祭 2024-12-06 02:02:13

在这两种情况下,最终列表 a 都是列表 b 的副本。但用于实现这一目标的方法已经改变。

a[:] = b 修改列表 a,使其具有与 b 相同的元素

a = b[:]< /code> 生成一个新列表,它是 b 的副本并替换列表 a

区别在于我们是修改了现有列表还是创建了新列表。

要查看差异:

a = range(3)
b = range(4)
c = a # c and a now share the same list
a[:] = b
print "a", a
print "b", b
print "C", c

所有三个列表都会打印出相同的结果。 C 和 a 共享同一个对象,因此当 a 被修改时,c 也被修改,

a = range(3)
b = range(4)
c = a # c and a now share the same list
a = b[:]
print "a", a
print "b", b
print "C", c

现在 c 不会打印出与 a 相同的内容。赋值后,ac 不共享同一个对象。

从速度上来说,a[:] = b 可能比 a = b[:] 快一点。第一种形式不必创建新的列表对象,它只需修改现有列表即可。其中很大一部分是它可以重用列表已经拥有的内存,而不是分配新的内存。

In both cases you end up the list a being a copy of the list b. But the method used to accomplish this has changed.

a[:] = b modifies the list a so that it has the same elements as b

a = b[:] produces a new list which is a copy of b and replaces the list a

The difference is whether we've modified an existing list or created a new one.

To see the difference:

a = range(3)
b = range(4)
c = a # c and a now share the same list
a[:] = b
print "a", a
print "b", b
print "C", c

All three lists will print out the same. C and a share the same object, so when a was modified so was c

a = range(3)
b = range(4)
c = a # c and a now share the same list
a = b[:]
print "a", a
print "b", b
print "C", c

Now c will not print out the same as a. After the assignment, a and c did not share the same object.

Speedwise, a[:] = b is probably a little faster than a = b[:]. The first form doesn't have to create a new list object, it can merely modify the existing list. A big part of this is that it can reuse the memory already owned by the list rather then allocating new memory.

行雁书 2024-12-06 02:02:12

[:] 是切片运算符。

当它位于左侧时,它会覆盖列表的内容而不创建新的引用。

当它位于右侧时,它会创建具有相同内容的列表的副本。

[:] is the slice operator.

When it's on the left side, it overwrites the contents of the list without creating a new reference.

When it's on the right side, it creates a copy of the list with the same contents.

云醉月微眠 2024-12-06 02:02:12

a = b[:] 调用 __getslice____getitem__b 并将结果分配给a。几乎在所有情况下(例如列表、元组和其他序列类型),这都会生成序列的浅表副本;我不知道有哪个类不实现该行为,但您可以有一个用户定义的类型来执行不同的操作。之前引用 a 旧值的任何其他对象将继续引用该旧值。

另一方面,a[:] = b 调用 __setslice____setitem__a 的元素子集替换为序列 b 的元素子集。在这种情况下,如果 a 的序列类型表现良好,这将替换整个 a,因为范围 : 没有端点表示整个序列。这里的区别在于,不可变类型(例如元组)不允许您执行 __setslice__ (例如,通过抛出 TypeError 异常)。之前引用 a 的任何其他对象也将被更新,因为底层对象正在被修改。

对于诸如 list 之类的可变类型,a = b[:] 的结果将与 a[:] = b 相同,因为a 将是 b 的浅拷贝;对于不可变类型(例如元组),a[:] = b 无效。对于行为不良的用户定义类型,所有的赌注都失败了。对于以 a 引用同一对象的其他对象,发生的情况也存在差异 - 对于 a = b[:],它们引用原始值 (< code>a),但使用 a[:] = b 时,它们引用修改后的对象(b 的浅拷贝)。

a = b[:] calls either __getslice__ or __getitem__ on b and assigns the result to a. In nearly all cases (e.g. lists, tuples, and other sequence types), this makes a shallow copy of the sequence; I don't know of any classes that don't implement that behavior, but you could have a user-defined type that did something different. Any other objects that previously referred to the old value of a will continue to refer to that old value.

a[:] = b, on the other hand, calls __setslice__ or __setitem__ to replace a subset of the elements of a with those of the sequence b. In this case, if the sequence type of a is well-behaved, this will replace the entirety of a, since the range : without endpoints indicates the entire sequence. The difference here is that immutable types, such as tuples, will not allow you to perform __setslice__ (e.g. by throwing a TypeError exception). Any other objects that previously referred to a will also be updated, since the underlying object is being modified.

For mutable types such as list, the result of a = b[:] will be identical to a[:] = b, in that a will be a shallow copy of b; for immutable types such as tuple, a[:] = b is invalid. For badly-behaved user-defined types, all bets are off. There's also a difference in what happens to other objects that referred to the same object as a -- with a = b[:], they refer to the original value (a), but with a[:] = b, they refer to the modified object (shallow copy of b).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文