Nested List Question

N

Newsfeeds

Hello All,

Could anyone tell me why this code produces the output it does?

noAdjacencies = 2
gridsPerAdj = 3
rows = 4
columns = 5


gridSystemId = [[None]*columns]*rows
for row in range(rows):
for column in range(columns):
gridSystemId[row][column] = "%d-%d" % (row,column)

print gridSystemId

Produces:

[['3-0', '3-1', '3-2', '3-3', '3-4'], ['3-0', '3-1', '3-2', '3-3', '3-4'],
['3-0
', '3-1', '3-2', '3-3', '3-4'], ['3-0', '3-1', '3-2', '3-3', '3-4']]

Rather than:

[['0-0', '0-1', '0-2', '0-3', '0-4'], ['1-0', '1-1', '1-2', '1-3', '1-4'],
['2-0
', '2-1', '2-2', '2-3', '2-4'], ['3-0', '3-1', '3-2', '3-3', '3-4']]

Thanks for your help,
Chris M.
 
A

Alex Martelli

Newsfeeds said:
Hello All,

Could anyone tell me why this code produces the output it does? ...
gridSystemId = [[None]*columns]*rows

You've made gridSystemID a list of `rows` references to the SAME "inner"
list, so the behavior you observe is the only possible one.

If you want copies instead, ASK for copies...:

gridSystemId = [ [None]*columns for x in xrange(rows) ]


Alex
 
C

Chris McCoy

Thank you! I've been banging my head against the wall!

Chris M.

Alex Martelli said:
Newsfeeds said:
Hello All,

Could anyone tell me why this code produces the output it does? ...
gridSystemId = [[None]*columns]*rows

You've made gridSystemID a list of `rows` references to the SAME "inner"
list, so the behavior you observe is the only possible one.

If you want copies instead, ASK for copies...:

gridSystemId = [ [None]*columns for x in xrange(rows) ]


Alex
 
R

Roman Suzi

Thank you! I've been banging my head against the wall!

Chris M.
gridSystemId = [[None]*columns]*rows

You've made gridSystemID a list of `rows` references to the SAME "inner"
list, so the behavior you observe is the only possible one.

If you want copies instead, ASK for copies...:

gridSystemId = [ [None]*columns for x in xrange(rows) ]


Interesting, could not pychecker recognize such situations in Python
code and give warnings?


Sincerely yours, Roman Suzi
 
C

Chris McCoy

It may, but I haven't been using Pychecker yet. I'm still fairly new to
Python.

Thanks,
Chris M.

Roman Suzi said:
Thank you! I've been banging my head against the wall!

Chris M.
gridSystemId = [[None]*columns]*rows

You've made gridSystemID a list of `rows` references to the SAME "inner"
list, so the behavior you observe is the only possible one.

If you want copies instead, ASK for copies...:

gridSystemId = [ [None]*columns for x in xrange(rows) ]


Interesting, could not pychecker recognize such situations in Python
code and give warnings?


Sincerely yours, Roman Suzi
 
M

Mike Meyer

Roman Suzi said:
gridSystemId = [[None]*columns]*rows
You've made gridSystemID a list of `rows` references to the SAME "inner"
list, so the behavior you observe is the only possible one.
If you want copies instead, ASK for copies...:
gridSystemId = [ [None]*columns for x in xrange(rows) ]
Interesting, could not pychecker recognize such situations in Python
code and give warnings?

Well, it could always just issue warnings everytime it saw a list
multiplied by something. But that would get annoying in the cases
where that idiom doesn't have problems - which is naturally most such
usages. Your example included one such, which is why the translation
wasn't to:

gridSystemId = [[None for y in xrange(columns)] for x in xrange(rows)] WRONG

[None] * columns doesn't have problems. Nor does any other immutable
object. So to avoid spurious warnings, pychecker would have to know
whether the objects in the list were immutable or not. It could guess
that if the objects are builtin types, but not for other types.

<mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,270
Messages
2,571,339
Members
48,029
Latest member
Anchorman2022

Latest Threads

Top