Converting a bidimensional list in a bidimensional array

Santiago Romero sromero at gmail.com
Wed Jan 9 09:26:05 EST 2008


> >  This is how I create the tilemap (and the clipboard, a copy of my
> > tilemap):
>
> >     def __init__( self, bw, bh, tiles ):
> >       self.tilemap = []
> >       (...)
> >       for i in range(bh):
> >          self.tilemap.append([0] * bw)

>        def __init__( self, bw, bh, tiles ):
>          self.width, self.height = bw, bh
>          self.tilemap = array.array('b', [0]) * bw * bh
>
> Gives a pure linearization (you do the math for lines).

 Do you mean : tilemap[(width*y)+x] ?

>        def __init__( self, bw, bh, tiles ):
>          self.width, self.height = bw, bh
>          self.tilemap = [array.array('b', [0]) * bw for row in range(bh)]
>
> Gives a list of arrays.  I punted on the type arg here; you should make
> a definite decision based on your data.

 What do you think about:

- Memory Performance: Of course, my maps will take ( 16 bytes / 2-4
bytes ) = 8 or 4 times less memory (32 / 64 bit processores) ...
right?

- Speed Performance: Do you think that changing from list to Array()
would improve speed? I'm going to do lots of tilemap[y][x] checks (I
mean, player jumping around the screen, checking if it's falling over
a non-zero tile, and so).

 And thanks a lot for your answer :-)



More information about the Python-list mailing list