Python二维数组-更改元素 [英] Python two-dimensional array - changing an element

查看:889
本文介绍了Python二维数组-更改元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个7x7二维数组:

I have this 7x7 two-dimensional array:

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

如您所见,l [1] [2] = 2.当我打印它时,该元素已正确打印.没问题但是,当我尝试将其从"2"更改为"3"或任何其他数字时,程序将更改该列(在本例中为第3列)中除第一和最后一个元素以外的所有元素.例如,如果我输入以下代码:

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

推荐答案

即使您描述的行为(如您所描述的)不可能,我也会对此采取行动.

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

例如

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

解决方法是不要在外部列表上使用* 7(内部列表是确定的,因为None是不可变的):

The fix is to not use the * 7 on the outer list (the inner list is OK since None is immutable):

a = [[None]*7 for _ in range(7)]

例如:

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

这篇关于Python二维数组-更改元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆