[Tutor] Minesweeper implementing 20%, weird problem with matrixes
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Thu Jul 14 00:43:42 CEST 2005
Hi Alberto,
The issue you're seeing is in the intialize() code:
######
def initialize(self):
self.buttonmatrix=[]
self.statematrix=[]
self.bombmatrix=[]
for i in range(self.fil):
aux=[]
for j in range(self.col):
aux.append(None)
self.buttonmatrix.append(aux)
self.bombmatrix.append(aux)
self.statematrix.append(aux)
self.bombs()
######
In particular, it's the reuse of the same list value who we will call
'aux'.
Let's try something from the interpreter to show a simplified version of
the problem:
######
>>> aux = []
>>> x = [aux, aux, aux]
>>> aux.append(42)
>>> aux
[42]
>>> x
[[42], [42], [42]]
######
The last result might be surprising to you. Things might be clearer if we
draw a diagram of what Python's universe looks like at this point:
---------------------------
aux --------->[42] <
^^ \
| \ \
| | |
x ---------> [ , , ]
----------------------------
My ascii art is terrible. *grin* But I hope it's a little clearer now
that what's happening is that the list's values are all to the same thing.
Does this make sense so far? Please feel free to ask more questions.
More information about the Tutor
mailing list