2-dimensional data structures

Claudio Grondi claudio.grondi at freenet.de
Thu Jan 26 22:46:00 CET 2006


anthonyberet wrote:
> Hello again - rather a newbie here...
> 
> I want to work on a sudoku brute-forcer, just for fun.
> 
> I am considering different strategies, but first I need to decide on the 
> data-structure to use for the progress/solution grid.
> 
> This being a square, I would have used a 9x9 2-dimensional array in my 
> teenage years back in the 80's, using BASIC.
> 
> What is the equivalent way to store data in python? - It isn't obvious 
> to me how to do it with lists.
> 
> 'scuse me for being thick - but give me a little pointer and I will do 
> the rest.

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)]

I haven't checked it out, but I can imagine, that this approach could 
even have a speed advantage over a list of lists what can become 
important in a 'brute-forcer' approach.

Best way is probably to use one of available numeric libraries with 
array support, but this is another story.

Claudio

Claudio



More information about the Python-list mailing list