By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,054 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

Nested List Question

P: n/a
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.

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 3 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Newsfeeds <ch*********@spirentcom.com> wrote:
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
Nov 3 '05 #2

P: n/a
Thank you! I've been banging my head against the wall!

Chris M.

"Alex Martelli" <al***@mail.comcast.net> wrote in message
news:1h5f4t2.1yv785h120xalfN%al***@mail.comcast.ne t...
Newsfeeds <ch*********@spirentcom.com> wrote:
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


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 3 '05 #3

P: n/a
On Thu, 3 Nov 2005, Chris McCoy wrote:
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
--
rn*@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
Nov 3 '05 #4

P: n/a
It may, but I haven't been using Pychecker yet. I'm still fairly new to
Python.

Thanks,
Chris M.

"Roman Suzi" <rn*@onego.ru> wrote in message
news:ma*************************************@pytho n.org...
On Thu, 3 Nov 2005, Chris McCoy wrote:
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
--
rn*@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 3 '05 #5

P: n/a
"Newsfeeds" <ch*********@spirentcom.com> wrote:
Could anyone tell me why this code produces the output it does?


http://www.python.org/doc/faq/progra...mensional-list

has the full story.

</F>

Nov 3 '05 #6

P: n/a
Roman Suzi <rn*@onego.ru> writes:
On Thu, 3 Nov 2005, Chris McCoy wrote:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 3 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.