Python list confusion [duplicate] - python

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 1 year ago.
Let's say I have the following code:
a_list = [[0]*10]*10
This generates the following list:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Then I want to modify the first element in the first list:
a_list[0][0] = 23
I expected only the first element of the list to be modified, but actually the first element of each list was changed:
[[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[23, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
I managed to find another way to represent my data to avoid this but why is this happening? Why isn't just the first list changed? When I do the second *10, does Python actually copy the first list's address instead of allocating a new memory block?

Your hunch about copying addresses is correct. Think about it like this:
sub_list = [0] * 10
a_list = [sub_list] * 10
This code is actually equivalent to the code you have posted above. What this means is that you are actually changing the same list sub_list whenever you change any element of a_list. You can even make sure of it by typing:
a_list = [[0] * 10] * 10
for n in a_list:
print id(n)
And it will show up the same for every element. To remedy this, you should use:
a_list = [[0] * 10 for _ in range(10)]
In order to create a new sublist for every element of a_list.

Lists contain references to objects. Multiplication on lists simply repeats the references (to the same objects!). While this is fine for immutable objects (like integers), what you are getting is multiple references to the same list.
Create separate lists with this pattern [[0]*10 for _ in xrange(10)].

Why isn't just the first list changed?
The reason is simple, there really is only 1 list, not 10 - just as you already suspected:
In [1]: [[0]*10]*10
Out[1]:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
In [2]: map(id, _)
Out[2]:
[54094624,
54094624,
54094624,
54094624,
54094624,
54094624,
54094624,
54094624,
54094624,
54094624]
If you want to create 10 lists, you can achieve this easily via an expression like
[[0]*10 for x in xrange(10)]

Related

Setting a value to one place in a list(b) which is in another list(a) sets that value in the same place in all the lists that are in list(a) [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 2 years ago.
Im trying to make an 9x9 array using nested lists and when I set am trying to set a single value for some reason it is setting that value in every row of that array. It starts with a list like this
[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
I did something like list[2][3]=1 and the list changed to
[[0,0,0,1,0],
[0,0,0,1,0],
[0,0,0,1,0],
[0,0,0,1,0],
[0,0,0,1,0]]
I can't figure out what I did wrong. My code is
def resetGrids():
gridHidden=[]
gridShown=[]
gridpart=[]
for i in range(9):
gridpart.append(0)
for i in range(9):
gridHidden.append(gridpart)
gridShown.append(gridpart)
for i in range(10):
searching=True
while searching:
checkX=random.randint(0,8)
checkY=random.randint(0,8)
print(checkX, checkY)
if gridHidden[checkY][checkX]!=10:
print(gridHidden[checkY][checkX])
gridHidden[checkY][checkX]=10
print("a")
#for a in range(-1,2):
# if checkY+a>=0 and checkY+a<=8:
# for b in range(-1,2):
# if checkX+b>=0 and checkX+b<=8:
# if gridHidden[checkY+a][checkX+b]!=10:
# gridHidden[checkY+a][checkX+b]+=1
searching=False
print("B")
print(gridHidden)
print("c")
print("d")
try to set as the inner lists copy() of the actual list, I guess that it happend because you actually pass a reference to the list.
replace to this lines:
gridHidden.append(gridpart.copy())
gridShown.append(gridpart.copy())
when I tried this, and added this part:
print(gridHidden)
gridHidden[1][2] = 3
print(gridHidden)
I got this:
[[0, 10, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 10, 0, 0, 10, 10, 0, 0, 10], [0, 0, 0, 0, 0, 0, 0, 10, 0], [0, 0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 10, 0, 0, 0], [0, 0, 0, 0, 10, 0, 0, 0, 0], [0, 10, 0, 0, 0, 0, 0, 0, 0]]
[[0, 10, 0, 0, 0, 0, 0, 0, 0], [0, 0, 3, 0, 0, 0, 0, 0, 0], [0, 10, 0, 0, 10, 10, 0, 0, 10], [0, 0, 0, 0, 0, 0, 0, 10, 0], [0, 0, 0, 10, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 10, 0, 0, 0], [0, 0, 0, 0, 10, 0, 0, 0, 0], [0, 10, 0, 0, 0, 0, 0, 0, 0]]

Multiple values change when changing a single value in a nested list [duplicate]

This question already has answers here:
List of lists changes reflected across sublists unexpectedly
(17 answers)
Closed 6 years ago.
I noticed a weird behavior when I am changing a value in a nested list grid which is initialized as such grid = [[0]*10]*10.
grid = [[0]*10]*10
grid[0][0] = 1
for l in grid:
print l
Output:
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
When I initialised grid as grid = [[0]*10 for x in range(10)].
It worked as expected.
grid = [[0]*10 for x in range(10)]
grid[0][0] = 1
for l in grid:
print l
Output:
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Does anyone know why this happened?
Thank you for your help!
When you create grid = [[0]*10]*10, you are actually creating an array of references to the same object (a list of [0]s). So when you change the object via one reference, this changes all entries in the list.
This is a very common Python 'gotcha' for beginners.

Printing a 2D game board

I have to make an othello game board and I was wondering how you make it so it prints the board properly. This is my code at the moment.
import collections
NONE = 0
BLACK = 'B'
WHITE = 'W'
BOARD_COLUMNS = int(input('How many board columns? '))
BOARD_ROWS = int(input('How many board rows? '))
class OthelloGameState:
def _new_game_board() -> [[int]]:
board = []
for col in range(BOARD_COLUMNS):
board.append([])
for row in range(BOARD_ROWS):
board[-1].append(NONE)
return board
print (_new_game_board())
I used the print to see what the board looks like and it came out like:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
How would I make it so it's supposed to be the way it's supposed to be?
import pprint
board = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
pprint.pprint(board)
Outputs:
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Check out a full run of the above:
https://ideone.com/6buCwM
You might want to make your board a state variable of the OthelloGameState class.
That way you can update that and your _new_game_board() method will just fill that with the proper size of rows and columns.
Then you can use Copy and Paste's answer to print the board (or your own method if you want to display it without the braces).
def print_board(board):
for col in range(BOARD_COLUMNS):
print(' '.join([row for row in range(BOARD_ROWS)]))
Python's print() method will not format your output at all, so you will need to do that yourself.
https://ideone.com/bNxew0 for a working example

Finding all legal moves in a simple board game

I have an 8x8 board represented by a numpy.ndarray:
array([[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]])
# 0 = free space
# 1 = player1's figure
A figure can either move forward and left, forward and right or just forward (forward means down the board in this case).
Right now I am using nested for loops in order to look through the board indexes. When I find a figure I append the board states that can be achieved by making moves with that figure to a list and then keep searching for figures.
For this example the output of my function looks like this:
[array([[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]]])]
Is there a faster way in which I can find all the possible moves for a given board state?
Wouldn't it be easier for memory as well as performance sake, to rather than keeping a complete board in the memory , keep the player figure's position in the memory. Lets take your example, player figure's location is -
player1 = (1, 4)
Let's assume , players position is denoted by (x,y) . Then you can compute the moves for that player at runtime (no need to keep in memory) , the possible moves would be -
(x+1,y)
(x+1,y+1)
(x+1,y-1)
If the player figure can circle back in the board, that is if he is at the bottom most position in the board, and then next moves would be the top most row , if that is the case, the moves would be determined by taking their modulo against the number of rows and columns (assuming nr - number of rows and nc - number of columns ) . Example , for (x,y) next moves would be -
((x+1) % nr,y % nc)
((x+1) % nr,(y+1) % nc)
((x+1) % nr,(y-1) % nc)

What is the difference between [[0 for _ in range(10)] for _ in range(10)] and [0 for _ in range(10)] * 10? [duplicate]

This question already has answers here:
Nested List Indices [duplicate]
(2 answers)
Closed 9 years ago.
>>> CM = [[0 for _ in range(10)]] * 10
>>> CM
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> CM[0][0] = CM[0][0] + 1
>>> CM
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
I was trying to create a confusion matrix. It basically contains count of the (i, j) pairs.
I first created a list of lists, and then incremented the appropriate variable. However, it didn't work as expected. CM[i][0] got incremented for all values of i.
I found a work around.
>>> CM = [[0 for _ in range(10)] for _ in range(10)]
>>> CM
[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
>>> CM[0][0] = CM[0][0] + 1
>>> CM
[[1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
But I would be grateful if someone could explain why the first method failed.
>>> CM = [[0 for _ in range(10)]] * 10
Is copying a reference to the same object, ten times. It is equivalent to this:
>>> x = [0 for _ in range(10)]
>>> CM = [x, x, x, x, x, x, x, x, x, x]
So manipulating one element causes side effects. Your workaround is elegant and correct.
Note:
This occurs since the elements of the lists are lists (which are mutable). If they were strings for example, which are immutable, it wouldn't be an issue if the same string was referenced in different lists, since they can't be manipulated. Python doesn't like to waste memory (unless explicitly told to ie. deepcopy), so copying lists will simply copy their references.

Categories