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!
Updating Values in Python 2D Array We can update a single element in a 2D array, by specifying its row and column index, and assigning that array position a new value. Assigning a new value overwrites the old value, thus updating it.
To shift the bits of array elements of a 2D array to the left, use the numpy. left_shift() method in Python Numpy. Bits are shifted to the left by appending x2 0s at the right of x1. Since the internal representation of numbers is in binary format, this operation is equivalent to multiplying x1 by 2**x2.
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]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With