473,802 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help, multidimensiona l list

Im trying to create a list of lists and for some reason its not working:

class Tile:
_next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
'sw':None, 'w':None, 'nw':None }
def blankGridCanvas ( maxx, maxy ):
grid = []
for y in xrange( 0, maxy ):
yline = []
for x in xrange( 0, maxx ):
t = Tile()
if y != 0:
t._next[ 'n' ] = grid[ y - 1 ][ x ]
t._next[ 'n' ]._next[ 's' ] = t

yline.append( t )
grid.append( yline )

for y in xrange( 0, maxy ):
for x in xrange( 0, maxx ):
print grid[ x ][ y ], grid[ x ][ y ]._next
return grid

now by my reconing this should be a list of lists with each element being
an instance of Tile, and some references being manipulated to point to each
other. But strangly no, its actually a list of lists where each element is
the SAME instance of Tile, so I change one, i change them ALL!

Whats going on and how do I solve it?

Rich
Jul 18 '05 #1
3 1823
On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
<cr***********@ IGetTooMuchSpam AsItIs.com> wrote:
Im trying to create a list of lists and for some reason its not working:

class Tile:
_next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
'sw':None, 'w':None, 'nw':None }
def blankGridCanvas ( maxx, maxy ):
grid = []
for y in xrange( 0, maxy ):
yline = []
for x in xrange( 0, maxx ):
t = Tile()
if y != 0:
t._next[ 'n' ] = grid[ y - 1 ][ x ]
t._next[ 'n' ]._next[ 's' ] = t

yline.append( t )
grid.append( yline )

for y in xrange( 0, maxy ):
for x in xrange( 0, maxx ):
print grid[ x ][ y ], grid[ x ][ y ]._next
return grid

now by my reconing this should be a list of lists with each element being
an instance of Tile,
You only create a *class* Tile, and provide no way to create
*instances* of it - so much like with static variables in other
languages, you only have one 'instance' here. To be able to create
separate instances, you need a constructor in your class to
instantiate every instance, like so:

class Tile:
def __init__(self):
self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
's':None, 'sw':None, 'w':None, 'nw':None }
and some references being manipulated to point to eachother. But strangly no, its actually a list of lists where each element is
the SAME instance of Tile, so I change one, i change them ALL!

Whats going on and how do I solve it?


Any gurus around can certainly much better explain what's going on and
why...

--
Christopher
Jul 18 '05 #2

"Christophe r Koppler" <kl******@chell o.at> wrote in message
news:jb******** *************** *********@4ax.c om...
On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
<cr***********@ IGetTooMuchSpam AsItIs.com> wrote:
Im trying to create a list of lists and for some reason its not working:

class Tile:
_next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
'sw':None, 'w':None, 'nw':None }
def blankGridCanvas ( maxx, maxy ):
grid = []
for y in xrange( 0, maxy ):
yline = []
for x in xrange( 0, maxx ):
t = Tile()
if y != 0:
t._next[ 'n' ] = grid[ y - 1 ][ x ]
t._next[ 'n' ]._next[ 's' ] = t

yline.append( t )
grid.append( yline )

for y in xrange( 0, maxy ):
for x in xrange( 0, maxx ):
print grid[ x ][ y ], grid[ x ][ y ]._next
return grid

now by my reconing this should be a list of lists with each element beingan instance of Tile,
You only create a *class* Tile, and provide no way to create
*instances* of it


No, the line 't=Tile()' does create a separate Tile for each grid position.
However, there is only one class attribute shared by all instances.
- so much like with static variables in other
languages, you only have one 'instance' here. To be able to create
separate instances, you need a constructor in your class to
instantiate every instance, like so:

class Tile:
def __init__(self):
self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
's':None, 'sw':None, 'w':None, 'nw':None }
You do not need __init__ for separate instances, but do need it to give
each instance its own map.
and some references being manipulated to point to each
other. But strangly no, its actually a list of lists where each element isthe SAME instance of Tile, so I change one, i change them ALL!


As stated above, you do have separate instances but only one map attached
to the class instead of a separate map for each instance. Koppler's
__init__ will fix this.

Terry J. Reedy
Jul 18 '05 #3
On Sun, 28 Dec 2003 17:50:07 -0500, "Terry Reedy" <tj*****@udel.e du>
wrote:

"Christophe r Koppler" <kl******@chell o.at> wrote in message
news:jb******* *************** **********@4ax. com...
On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
<cr***********@ IGetTooMuchSpam AsItIs.com> wrote:
>Im trying to create a list of lists and for some reason its not working:
>
>class Tile:
> _next = { 'n':None, 'ne':None, 'e':None, 'se':None, 's':None,
>'sw':None, 'w':None, 'nw':None }
>
> [snip some code] >
>now by my reconing this should be a list of lists with each elementbeing >an instance of Tile,
You only create a *class* Tile, and provide no way to create
*instances* of it


No, the line 't=Tile()' does create a separate Tile for each grid position.
However, there is only one class attribute shared by all instances.


Yes, thanks for clearing that up. That is what I probably meant and
didn't know how to say. Too merry Xmas this year ;-)
- so much like with static variables in other
languages, you only have one 'instance' here. To be able to create
separate instances, you need a constructor in your class to
instantiate every instance, like so:

class Tile:
def __init__(self):
self._next = { 'n':None, 'ne':None, 'e':None, 'se':None,
's':None, 'sw':None, 'w':None, 'nw':None }
You do not need __init__ for separate instances, but do need it to give
each instance its own map.


I think that's what I meant with the comparison to static variables,
but should have read static class attributes.
and some references being manipulated to point to each
>other. But strangly no, its actually a list of lists where each elementis >the SAME instance of Tile, so I change one, i change them ALL!


As stated above, you do have separate instances but only one map attached
to the class instead of a separate map for each instance. Koppler's
__init__ will fix this.

Terry J. Reedy

--
Christopher
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1350
by: Nils Grimsmo | last post by:
i always have trouble explaining why creation of multidimensional lists is not as straight forward as it could be in python. list comprehensions are ugly if you are new to the language. i really would like to see it made easy. i propose using tuples in the same way as integers are now. example: >>> * (2,3,4) , , ], , , ]] this is even less "mathematically bad" syntax than multiplication with
3
2652
by: doodle4 | last post by:
Hello all, I am trying to convert some C code into python. Since i am new to python, i would like to know how to deal with multidimensional arrays? Thanks, -Joe Here's a snippet of what i am trying to convert:
1
2039
by: Gabriel Birke | last post by:
Given the multidimensional list l: l = ] ] I want to access specific items the indices of which are stored in another list. For now, I created a function to do this: def getNestedValue(l, indices): while len(indices) > 0:
1
8288
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a RankException. But the MSDN documentation says: When copying between multidimensional arrays, the array
3
1348
by: BobbyS | last post by:
I am trying to develop a multidimensional array for use of searching a very large database. I understand the concept of one and two dimensional arrays but this project would include up to 12 or 13 criteiras for the search. The engine needs to be user friendly for general public use so the SQL/query path method is not really an option for me. I have heard or read about multidimensional cube arrays and am not sure if this is a viable method...
8
2893
by: Chuck Bowling | last post by:
I have a list: List<RichTextBoxrtb; That represents an NxM matrix of rtb's. Is there some way to initialize a generic that will allow me to access the List like a multidimensional array i.e.; rtb.Text = "some text";
16
3627
by: Rehceb Rotkiv | last post by:
Hello everyone, can I sort a multidimensional array in Python by multiple sort keys? A litte code sample would be nice! Thx, Rehceb
3
4606
by: luftikus143 | last post by:
Hi there, I need to store three pieces of data - a result of a SQL query - in a multidimensional array, but don't really succeed. Sometimes it works, but then the output doesn't work accordingly. My SQL result comes with something like this: Africa | y2000 | 9430 Africa | y2005 | 9678 Europe | y2000 | 2314 where there are 6 regions and multiple years.
6
1958
by: themadme | last post by:
hi, im trying to create a multidimensional array and then pass along a few funcitons. this is the way i have created, im sure its the correct way of doing it? // TerrainMapData is struct TerrainMapData **TerrainData ; TerrainData = new TerrainMapData *;
9
4503
by: Slain | last post by:
I need to convert a an array to a multidimensional one. Since I need to wrok with existing code, I need to modify a declaration which looks like this In the .h file int *x; in a initialize function: x = new int;
0
10538
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6838
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3792
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.