I have this 7x7 two-dimensional array:
我有这个7x7二维数组
l=[[1, 1, 1, 1, 1, 1, 1],
[1, 0, 2, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]]
As you can see, l[1][2]=2. When I print it, the element is printed correctly. No problem here. But when I try to change it from "2" to "3" or any other number, the program changes all the elements on that column (in this case the 3rd column) except for the first and last ones. For example, if I type this code:
可以看到,l[1][2]=2。当我打印它时,元素被正确地打印出来。这里没有问题。但是当我尝试将它从“2”更改为“3”或任何其他数字时,程序会更改该列上的所有元素(在本例中是第3列),除了第一列和最后一列。例如,如果我输入以下代码:
l[1][2]=5
and then print the two-dimensional array, I get this:
然后打印二维数组,我得到这个:
l=[[1, 1, 1, 1, 1, 1, 1],
[1, 0, 5, 0, 0, 0, 1],
[1, 0, 5, 0, 0, 0, 1],
[1, 0, 5, 0, 0, 0, 1],
[1, 0, 5, 0, 0, 0, 1],
[1, 0, 5, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]]
This happens with every element that I choose. Instead of changing only that element, it changes the entire column. Does anyone know what might be the problem? Thank you!
这发生在我选择的每一个元素上。它不仅改变了这个元素,还改变了整个列。有人知道问题出在哪里吗?谢谢你!
2 个解决方案
#1
21
I'm gonna take a stab at this one even though the behavior you describe (as you've described it) isn't possible.
尽管你描述的行为(正如你描述的那样)是不可能的,但我还是想尝试一下。
If you create a list, you need to make sure that each sublist is a different list. Consider:
如果您创建了一个列表,您需要确保每个子列表都是一个不同的列表。考虑:
a = []
b = [a, a]
Here I've created a list where both of the sublists are the exact same list. If I change one, it will show up in both. e.g.:
这里我创建了一个列表,其中两个子列表都是完全相同的列表。如果我换一个,它会同时出现在这两个地方。例如:
>>> a = []
>>> b = [a, a]
>>> b[0].append(1)
>>> b
[[1], [1]]
you'll frequently see this behavior with a list initialized using the *
operator:
您将经常看到使用*操作符初始化的列表:
a = [[None]*7]*7
e.g.
如。
>>> a = [[None]*7]*7
>>> a
[[None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None]]
>>> a[0][1] = 3
>>> a
[[None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None]]
The fix is to not use the *
7 on the outer list (the inner list is OK since None
is immutable):
修正是不要使用外部列表上的* 7(内部列表是可以的,因为没有一个是不可变的):
a = [[None]*7 for _ in range(7)]
e.g.:
例如:
>>> a = [[None]*7 for _ in range(7)]
>>> a[0][1] = 3
>>> a
[[None, 3, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, 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
1
You've constructed your list wrong.
你把你的清单写错了。
The middle items are all referring to the same list, so updating one causes the change to be reflected in the others
中间的项目都指向相同的列表,因此更新一个将导致更改反映到其他的列表中
If you show the code you're using to construct the list, I can show you how to fix it.
如果您显示用于构建列表的代码,我可以向您展示如何修复它。
Alternatively
另外
l = [sublist[:] for sublist in l]
before you begin modifying the lists will decouple all those into new lists
在您开始修改列表之前,将把所有这些分离到新的列表中
#1
21
I'm gonna take a stab at this one even though the behavior you describe (as you've described it) isn't possible.
尽管你描述的行为(正如你描述的那样)是不可能的,但我还是想尝试一下。
If you create a list, you need to make sure that each sublist is a different list. Consider:
如果您创建了一个列表,您需要确保每个子列表都是一个不同的列表。考虑:
a = []
b = [a, a]
Here I've created a list where both of the sublists are the exact same list. If I change one, it will show up in both. e.g.:
这里我创建了一个列表,其中两个子列表都是完全相同的列表。如果我换一个,它会同时出现在这两个地方。例如:
>>> a = []
>>> b = [a, a]
>>> b[0].append(1)
>>> b
[[1], [1]]
you'll frequently see this behavior with a list initialized using the *
operator:
您将经常看到使用*操作符初始化的列表:
a = [[None]*7]*7
e.g.
如。
>>> a = [[None]*7]*7
>>> a
[[None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None]]
>>> a[0][1] = 3
>>> a
[[None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None], [None, 3, None, None, None, None, None]]
The fix is to not use the *
7 on the outer list (the inner list is OK since None
is immutable):
修正是不要使用外部列表上的* 7(内部列表是可以的,因为没有一个是不可变的):
a = [[None]*7 for _ in range(7)]
e.g.:
例如:
>>> a = [[None]*7 for _ in range(7)]
>>> a[0][1] = 3
>>> a
[[None, 3, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, None, None, None], [None, None, None, None, 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
1
You've constructed your list wrong.
你把你的清单写错了。
The middle items are all referring to the same list, so updating one causes the change to be reflected in the others
中间的项目都指向相同的列表,因此更新一个将导致更改反映到其他的列表中
If you show the code you're using to construct the list, I can show you how to fix it.
如果您显示用于构建列表的代码,我可以向您展示如何修复它。
Alternatively
另外
l = [sublist[:] for sublist in l]
before you begin modifying the lists will decouple all those into new lists
在您开始修改列表之前,将把所有这些分离到新的列表中