2-dimensional data structures

Max rabkin at mweb[DOT]co[DOT]za
Fri Jan 27 15:50:16 EST 2006


Claudio Grondi wrote:
> 
> Another approach as already proposed could be, that you define your grid 
> as a dictionary in a following way:
> grid = {}
> for column in range(1,10):
>   for row in range(1,10):
>     grid[(column, row)] = None
> # then you can refer to the cells of the 'array' like:
> colNo=5; rowNo=4
> valueInCellOfGrid = grid[(colNo, rowNo)]
> # and set them like:
> grid[(colNo, rowNo)] = 9
> print valueInCellOfGrid
> print grid[(colNo, rowNo)]
> 

FWIW, if you leave out the parentheses, it looks more like a genuine 2D 
array, which can be nice (actually I think it's the main advantage over 
lists of lists):

print grid[colNo, rowNo]

> 
> Claudio

--Max



More information about the Python-list mailing list