在尝试修改单个值时,2D列表具有奇怪的行为[重复]

时间:2022-03-13 21:51:48

Possible Duplicate:
Unexpected feature in a Python list of lists

可能重复:Python列表中的意外功能

So I am relatively new to Python and I am having trouble working with 2D Lists.

所以我对Python比较新,我在处理2D列表时遇到了麻烦。

Here's my code:

这是我的代码:

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

and here is the output (formatted for readability):

这是输出(为便于阅读而格式化):

[['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 every row get assigned the value?

为什么每一行都被分配了值?

3 个解决方案

#1


50  

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 does what you expect:

现在它符合您的期望:

>>> 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]]

#2


10  

As the python library reference for sequence types, which includes lists, says

作为序列类型的python库引用,包括列表,说

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

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

>>> 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.

发生的事情是[[]]是一个包含空列表的单元素列表,因此[[]] * 3的所有三个元素都是(指向)这个空列表。修改列表的任何元素都会修改此单个列表。

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]]

#3


2  

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

在python中,每个变量都是一个对象,因此是一个引用。您首先创建了一个包含5个Nones的数组,然后构建一个具有相同对象5倍的数组。

#1


50  

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 does what you expect:

现在它符合您的期望:

>>> 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]]

#2


10  

As the python library reference for sequence types, which includes lists, says

作为序列类型的python库引用,包括列表,说

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

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

>>> 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.

发生的事情是[[]]是一个包含空列表的单元素列表,因此[[]] * 3的所有三个元素都是(指向)这个空列表。修改列表的任何元素都会修改此单个列表。

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]]

#3


2  

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

在python中,每个变量都是一个对象,因此是一个引用。您首先创建了一个包含5个Nones的数组,然后构建一个具有相同对象5倍的数组。