[Tutor] Pythonic nested lists
alan.gauld at btinternet.com
Mon Oct 4 09:17:04 CEST 2010
"col speed" <ajarncolin at gmail.com> wrote
> HI again, I realise that I should have included more information in
> above e-mail. Here goes:
> a/ I have a 9*9 nested list called "rows", which contains the given
> in a sudoku puzzle - with "0"s where the blanks go.
> b/ I create roworder (roworder = [[str(j)+str(i) for i in range(9)]
> for j in
> range(9)]) - [["00" --> "08"], ["10" --> "18"] --> ["80" -->
> c/ I populate a dictionary ("dic") with keys from "roworder" and
> values from
> d/ I loop through the dict, changing any value"0" to be set(range(1,
> 10))- a
> list of "possible numbers".
> e/ Then I do:
> for i in roworder:
> notPoss = set([dic[j] for j in i if type(dic[j]) == int])
> for k in j:
> if type(dic[k]) == set:
> thus removing the numbers that exist in the row from the "possible
> I need to do this for the columns and squares aswell, which is why I
That approach should work.
Personally I'd probably create a Square class and bae my data on 9
Each square can return a row(of 3 items) given an index and a
column(of 3 items)
given an index.
def __init__(self, size=3): self.cells = [0 for n in
def getRow(self,n): ...
Thus I'd build my rows and columns dynamically from the squares rather
than the other way round. It just feels more natural to me.
But your approach should work too.
However I confess I haven't studied you code in detail.
Is there any specific problem or question you have or are you looking
for general feedback?
Author of the Learn to Program web site
More information about the Tutor