Converting a bidimensional list in a bidimensional array

Scott David Daniels Scott.Daniels at Acm.Org
Wed Jan 9 09:06:40 EST 2008


Santiago Romero wrote:
>  I'm trying to change my current working source code so that it works
> with an array instead of using a list, but I'm not managing to do it.
> ...
> 
>  This is how I create the tilemap (and the clipboard, a copy of my
> tilemap):
> 
>     def __init__( self, bw, bh, tiles ):
>       self.width, self.height = bw, bh
>       self.tilemap = []
>       self.clipboard = []
>       (...)
>       for i in range(bh):
>          self.tilemap.append([0] * bw)
>          self.clipboard.append([0] * bw)

       def __init__( self, bw, bh, tiles ):
         self.width, self.height = bw, bh
         self.tilemap = array.array('b', [0]) * bw * bh
         self.clipboard = array.array('b', self.tilemap)

Gives a pure linearization (you do the math for lines).

       def __init__( self, bw, bh, tiles ):
         self.width, self.height = bw, bh
         self.tilemap = [array.array('b', [0]) * bw for row in range(bh)]
         self.clipboard = [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.

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list