While writing a code snippet for a Leetcode question, I noticed that my Python3 arrays were not getting updated as expected.
Basically, I have a 2D array listing players moves for a tic-tac-toe game. Player A plays first, Player B next, Player A next, and so on. My first move is to represent these moves on a 2D array. The array moves listed below mentions the (x,y) co-ordinates of the 3x3 TicTacToe grid that is to be updated.
(0,0) | (0,1) | (0,2)
_______|_________|________
(1,0) | (1,1) | (1,2)
| |
_______|_________|________
(2,0) | (2,1) | (2,2)
| |
moves = [[0,0],[2,0],[1,1],[2,1],[2,2]]
arr=[['']*3]*3
#print(arr)
for i in range(len(moves)):
xC=moves[i][0]
yC=moves[i][1]
if(i%2==0):
#playerA
arr[xC][yC]='X'
else:
#playerB
arr[xC][yC]='O'
print(arr)
The variable 'moves' lists the population of only 5 boxes out of 9 boxes in the tic tac toe game, but the output array 'arr' is populated as such:
[['O', 'O', 'X'], ['O', 'O', 'X'], ['O', 'O', 'X']]
Just fyi: I've been using Py3 for a few years now, and I'm comfortable with it (not a newbie). What's your thoughts on the issue? A bug in Py3's array module? Let me know. (btw, my first StackOverflow Question!)
The question is at this address: https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/
-
Please correct your code. The int variable i would not allow subscripting.janus235– janus2352020年03月09日 10:29:59 +00:00Commented Mar 9, 2020 at 10:29
-
1Thanks! I have updated the same.Aswin Tekur– Aswin Tekur2020年03月09日 10:59:02 +00:00Commented Mar 9, 2020 at 10:59
1 Answer 1
The problem with your code is in the line arr=[['']*3]*3. It seems like you're creating three different lists of empty strings, but you're actually creating three references to the same list - that's what list multiplication does in python. When you change one list, they all change because they are all the same list.
Your code would run as expected if you replace the mentioned line with arr = [['']*3 for _ in range(3)]. This list comprehension will actually create three unrelated lists. Note that you're still creating three references to the same string in each list, as multiplication is still used - but that's okay because string are immutable in python, so it doesn't really matter.