2-dimensional data structures

anthonyberet nospam at me.invalid
Sat Feb 18 17:05:59 EST 2006


Tim Chase wrote:
>> I want to work on a sudoku brute-forcer, just for fun.
> 
> 
> Well, as everybody seems to be doing these (self included...), the 
> sudoku solver may become the "hello world" of the new world :)
> 
>> What is the equivalent way to store data in python? - It isn't obvious 
>> to me how to do it with lists.
> 
> 
> Several other answers have crossed the list.  I've done it using a 
> dictionary of tuples:
> 
>     grid = {}
>     for row in range(1,10):
>         for col in range(1,10):
>             grid[(row,col)] = value
> 
>     item = grid[(3,2)]
> 
> etc.
> 
> Seemed fairly quick and worked for me.
> 
Thanks for the advice (to everyone in the thread).
I think I will go with nested lists.
However, I am running into a conceptual problem.
My approach will be firstly to remove all the impossible digits for a 
square by searching the row and column for other occurances.

However, I wondering how to approach the search of the nine regions of 
the grid. I am thinking of producing another nested list, again 9x9 to 
store the contents of each region, and to update this after each pass 
through -and update of- the main grid (row and column).

I am not sure how to most efficiently identify which region any given 
square on the grid is actually in - any thoughts, for those that have 
done this? - I don't want a massive list of IF conditionals if I can 
avoid it.





More information about the Python-list mailing list