python输入二维数组_Python二维数组-更改元素

1586010002-jmsa.png

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

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

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):

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值