尝试修改单个值时,二维列表有奇怪的行为

发布于 2024-08-30 09:44:08 字数 382 浏览 5 评论 0原文

当我尝试此代码时:

data = [[None]*5]*5
data[0][0] = 'Cell A1'

data 的值最终如下所示:

[['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None]]

为什么 'Cell A1' 值出现在每个嵌套列表中?

When I try this code:

data = [[None]*5]*5
data[0][0] = 'Cell A1'

The value of data ends up like:

[['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None],
 ['Cell A1', None, None, None, None]]

Why does the 'Cell A1' value appear in every nested list?

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

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

发布评论

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

评论(3

丶视觉 2024-09-06 09:44:08

这将创建一个包含五个对相同列表的引用的列表:

data = [[None]*5]*5

使用类似这样的内容来创建五个单独的列表:

>>> data = [[None]*5 for _ in range(5)]

现在它的行为符合预期:

>>> data[0][0] = 'Cell A1'
>>> print(data)
[['Cell A1', None, None, None, None],
 [None, None, None, None, None],
 [None, None, None, None, None],
 [None, None, None, None, None],
 [None, None, None, None, None]]

This makes a list with five references to the same list:

data = [[None]*5]*5

Use something like this instead which creates five separate lists:

>>> data = [[None]*5 for _ in range(5)]

Now it behaves as expected:

>>> data[0][0] = 'Cell A1'
>>> print(data)
[['Cell A1', None, None, None, None],
 [None, None, None, None, None],
 [None, None, None, None, None],
 [None, None, None, None, None],
 [None, None, None, None, None]]
我家小可爱 2024-09-06 09:44:08

正如 中所述序列类型的文档(包括列表):

另请注意,副本是浅的;嵌套结构不会被复制。这常常困扰着新的 Python 程序员;考虑:

<前><代码>>>>列表 = [[]] * 3
>>>>>列表
[[]、[]、[]]
>>>>>列表[0].append(3)
>>>>>列表
[[3]、[3]、[3]]

发生的情况是 [[]] 是一个包含空列表的单元素列表,因此 [[]] * 3 的所有三个元素都是 (指向)这个单个空列表。修改 lists 的任何元素都会修改此单个列表。您可以通过以下方式创建不同列表的列表:

<前><代码>>>>列表 = [[] for i in range(3)]
>>>>>列表[0].append(3)
>>>>>列表[1].append(5)
>>>>>列表[2].append(7)
>>>>>列表
[[3]、[5]、[7]]

As explained in the documentation for sequence types (which includes lists):

Note also that the copies are shallow; nested structures are not copied. This often haunts new Python programmers; consider:

>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]

What has happened is that [[]] is a one-element list containing an empty list, so all three elements of [[]] * 3 are (pointers to) this single empty list. Modifying any of the elements of lists modifies this single list. You can create a list of different lists this way:

>>> lists = [[] for i in range(3)]  
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]
孤云独去闲 2024-09-06 09:44:08

在 Python 中,每个变量都是一个对象,因此也是一个引用。您首先创建了一个包含 5 个 None 的列表,然后构建了一个包含 5 个相同对象的列表。

In Python, every variable is an object, and thus a reference. You first created a list of 5 Nones, and then you build a list with 5 times the same object.

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