2-dimensional data structures

Scott David Daniels scott.daniels at acm.org
Fri Jan 27 16:51:22 EST 2006


Claudio Grondi wrote:
> anthonyberet wrote:
>> Hello again - rather a newbie here...
>> I am considering different strategies, but first I need to decide on 
>> the data-structure to use for the progress/solution grid.
> 
> ... 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)]

Though a couple of people have suggested a dictionary, nobody has
yet pointed out that a[i, j] is the same as a[(i, j)] (and looks
less ugly).  So, I'd go with dictionaries, and index them as
     grid = {}
     for column in range(1,10):
         for row in range(1,10):
             grid[column, row] = None
    ...
    valueInCellOfGrid = grid[col, row]
    grid[col, row] = 9
    ...

--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list