Problem with lists
Rafael David
rdavid.br at gmail.com
Tue Sep 15 20:29:14 EDT 2015
Em terça-feira, 15 de setembro de 2015 21:11:38 UTC-3, MRAB escreveu:
> On 2015-09-16 00:45, Rafael David wrote:
> > Hi guys,
> >
> > I'm newbie in Python (but not a newbie developer). I'm facing a
> > problem with a bidimensional list (list of lists) containing
> > dictionaries. I don't know if I didn't understand how lists and
> > dictionaries work in Python or if there is a mistake in my code that
> > I can't see. In the code I'm printing the list values just after the
> > assignment and they are ok, but when I try to print the list at the
> > end of the function the values are different (repeated). Below is
> > the code and the result in Python 3.4.0. Could you guys please help
> > me with that?
> >
> > Thanks a lot!
> >
> > Rafael
> >
> >>>> def preencherTabuleiroInicial():
> > tabuleiro = [[None for x in range(8)] for y in range(8)]
> > peca = {}
> > for lin in range(8):
> > for col in range(8):
> > if lin == 0 or lin == 1 or lin == 6 or lin == 7:
> > if lin == 0 or lin == 1:
> > peca['cor'] = 'b'
> > elif lin == 6 or lin == 7:
> > peca['cor'] = 'p'
> > if lin == 1 or lin == 6:
> > peca['nome'] = 'p'
> > elif col == 0 or col == 7:
> > peca['nome'] = 't'
> > elif col == 1 or col == 6:
> > peca['nome'] = 'c'
> > elif col == 2 or col == 5:
> > peca['nome'] = 'b'
> > elif col == 3:
> > peca['nome'] = 'd'
> > else:
> > peca['nome'] = 'r'
> > tabuleiro[lin][col] = peca
> > print(str(lin) + ' ' + str(col) + ' ' + str(tabuleiro[lin][col]))
> > print()
> > print(tabuleiro)
> >
> >>>>
> >>>> preencherTabuleiroInicial()
> > 0 0 {'nome': 't', 'cor': 'b'}
> > 0 1 {'nome': 'c', 'cor': 'b'}
> > 0 2 {'nome': 'b', 'cor': 'b'}
> > 0 3 {'nome': 'd', 'cor': 'b'}
> > 0 4 {'nome': 'r', 'cor': 'b'}
> > 0 5 {'nome': 'b', 'cor': 'b'}
> > 0 6 {'nome': 'c', 'cor': 'b'}
> > 0 7 {'nome': 't', 'cor': 'b'}
> > 1 0 {'nome': 'p', 'cor': 'b'}
> > 1 1 {'nome': 'p', 'cor': 'b'}
> > 1 2 {'nome': 'p', 'cor': 'b'}
> > 1 3 {'nome': 'p', 'cor': 'b'}
> > 1 4 {'nome': 'p', 'cor': 'b'}
> > 1 5 {'nome': 'p', 'cor': 'b'}
> > 1 6 {'nome': 'p', 'cor': 'b'}
> > 1 7 {'nome': 'p', 'cor': 'b'}
> > 2 0 None
> > 2 1 None
> > 2 2 None
> > 2 3 None
> > 2 4 None
> > 2 5 None
> > 2 6 None
> > 2 7 None
> > 3 0 None
> > 3 1 None
> > 3 2 None
> > 3 3 None
> > 3 4 None
> > 3 5 None
> > 3 6 None
> > 3 7 None
> > 4 0 None
> > 4 1 None
> > 4 2 None
> > 4 3 None
> > 4 4 None
> > 4 5 None
> > 4 6 None
> > 4 7 None
> > 5 0 None
> > 5 1 None
> > 5 2 None
> > 5 3 None
> > 5 4 None
> > 5 5 None
> > 5 6 None
> > 5 7 None
> > 6 0 {'nome': 'p', 'cor': 'p'}
> > 6 1 {'nome': 'p', 'cor': 'p'}
> > 6 2 {'nome': 'p', 'cor': 'p'}
> > 6 3 {'nome': 'p', 'cor': 'p'}
> > 6 4 {'nome': 'p', 'cor': 'p'}
> > 6 5 {'nome': 'p', 'cor': 'p'}
> > 6 6 {'nome': 'p', 'cor': 'p'}
> > 6 7 {'nome': 'p', 'cor': 'p'}
> > 7 0 {'nome': 't', 'cor': 'p'}
> > 7 1 {'nome': 'c', 'cor': 'p'}
> > 7 2 {'nome': 'b', 'cor': 'p'}
> > 7 3 {'nome': 'd', 'cor': 'p'}
> > 7 4 {'nome': 'r', 'cor': 'p'}
> > 7 5 {'nome': 'b', 'cor': 'p'}
> > 7 6 {'nome': 'c', 'cor': 'p'}
> > 7 7 {'nome': 't', 'cor': 'p'}
> >
> > [[{'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}], [{'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}], [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], [{'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}], [{'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'no
> me'
> > : 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}, {'nome': 't', 'cor': 'p'}]]
> >>>>
> >
> You're creating a dict:
>
> peca = {}
>
> putting entries into it:
>
> peca['cor'] = ...
>
> or:
> peca['nome'] = ...
>
> and then putting it into one of the lists of lists:
>
> tabuleiro[lin][col] = peca
>
> but it's the same dict; you're re-using it.
Oooohhh ... I think I got it! I'm assigning a reference to peca and not the value itself! Thank you very much MRAB and C Smith for the enlightenment :)
Rafael
More information about the Python-list
mailing list