473,387 Members | 1,541 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,387 software developers and data experts.

Help, multidimensional 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 1787
On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
<cr***********@IGetTooMuchSpamAsItIs.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

"Christopher Koppler" <kl******@chello.at> wrote in message
news:jb********************************@4ax.com...
On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
<cr***********@IGetTooMuchSpamAsItIs.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.edu>
wrote:

"Christopher Koppler" <kl******@chello.at> wrote in message
news:jb********************************@4ax.com.. .
On Sun, 28 Dec 2003 14:27:41 +0000, Crawley
<cr***********@IGetTooMuchSpamAsItIs.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
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...
3
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...
1
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,...
1
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...
3
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...
8
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.;...
16
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
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....
6
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 ...
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.