473,395 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Nested List Question

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
6 1493
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Joel Forrest Moxley | last post by:
Greetings python-list! The good news is that I've been having a blast with Python since early Spring. I've had great success in both learning the language from online / usegroup resources and...
1
by: Joel | last post by:
python-list@python.org at Sun, 24 Aug 2003 13:31:58 but didn't seem to show up here] Greetings python-list! The good news is that I've been having a blast with Python since early Spring. ...
6
by: Andy Baker | last post by:
Hi there, I'm learning Python at the moment and trying to grok the thinking behind it's scoping and nesting rules. I was googling for nested functions and found this Guido quote:...
15
by: Xah Lee | last post by:
Here's the belated Java solution. import java.util.List; import java.util.ArrayList; import java.lang.Math; class math { public static List range(double n) { return range(1,n,1); }
14
by: theo | last post by:
if I have nested div combinations, can I call for styles only to specific nested combos? It's 3 lists <li>, on one page, needing different styles. <div id=list1><li> <a id="t1"...
5
by: Atley | last post by:
I am trying to set up a nested collection so I can run statements like this: me.lblquestions.text = mysteps.item(1).questions(1).Asked Any ideas on how to do this? I have created a...
6
by: deko | last post by:
How do I construct an XHTML-compliant nested unordered list? This displays correctly (both FF and IE): <ul> <li>list item</li> <li>list item</li> <li>list item</li> <ul> <li>nested list...
2
by: brad | last post by:
Group, I'm using Visual Studio 2003 to create an ASP.NET 1.1 project which contains nested server user controls in order to create a tree-like hierarchy. The tree is a sort of question and...
7
by: patrick j | last post by:
Hi I'm wondering about lists with nested lists as one does on a Saturday afternoon. Anyway below is an example of a list with a nested list which the iCab browser's very useful HTML...
1
by: Nike | last post by:
I have a small question w.r.t nested class.. I see in many codes nested classes are declared as public.. First, of all my understanding is that you go in for a nested class if you are sure that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.