Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python two-dimensional array - changing an element [closed]

Tags:

python

arrays

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!

like image 263
Joker Avatar asked Jan 10 '14 04:01

Joker


People also ask

How do you update a 2D array in python?

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.

How do you shift elements in a 2D array?

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.


1 Answers

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]] 
like image 164
mgilson Avatar answered Oct 02 '22 14:10

mgilson