Re: [Tutor] Pythonic nested lists
col speed ajarnco...@gmail.com wrote HI again, I realise that I should have included more information in the above e-mail. Here goes: a/ I have a 9*9 nested list called rows, which contains the given numbers in a sudoku puzzle - with 0s 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 -- 88]] c/ I populate a dictionary (dic) with keys from roworder and values from rows d/ I loop through the dict, changing any value0 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: dic[k].difference_update(notPoss) thus removing the numbers that exist in the row from the possible numbers I need to do this for the columns and squares aswell, which is why I do: That approach should work. Personally I'd probably create a Square class and bae my data on 9 squares. Each square can return a row(of 3 items) given an index and a column(of 3 items) given an index. class Square: def __init__(self, size=3): self.cells = [0 for n in range(size*size)] ... def __str__(self):... def getRow(self,n): ... def getCol(self,n):... def setCell(self,x,y,value) def check(self):... 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? HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Pythonic nested lists
Message: 6 Date: Tue, 28 Sep 2010 13:15:26 +0700 From: col speed ajarnco...@gmail.com To: tutor@python.org Subject: [Tutor] Pythonic nested lists Message-ID: aanlktimdykbkzxaacbaagpq_faz50ruy=bcr81dqx...@mail.gmail.com Content-Type: text/plain; charset=iso-8859-1 Hi all, I've been trying to write a programme that solves sudoku problems for a while now. I'm getting close, but would like to ask a few questions about the most pythonic way of doing some things. I've decided to create nested lists (row order, column order and square order) which correspond with dictionary keys - the values of which are the numbers given in the puzzle - so I can loop through the dict in different orders. I think the first two are OK, but the square order list seems extremely messy and I would love some pointers. What I have is as follows: roworder = [[str(j)+str(i) for i in range(9)] for j in range(9)] colorder = zip(*roworder) #here comes the problem! start, index = 0,0 sqorder = [] for i in range(9): sqorder.append([]) for j in range(start, start+3): sqorder[i].extend(roworder[j][index:index+3]) if index == 6: index = 0 else: index += 3 if i == 2 or i == 5: start += 3 Any comments at all would be gratefully received. Thanks in advance Colin ___ HI again, I realise that I should have included more information in the above e-mail. Here goes: a/ I have a 9*9 nested list called rows, which contains the given numbers in a sudoku puzzle - with 0s 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 -- 88]] c/ I populate a dictionary (dic) with keys from roworder and values from rows d/ I loop through the dict, changing any value0 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: dic[k].difference_update(notPoss) thus removing the numbers that exist in the row from the possible numbers I need to do this for the columns and squares aswell, which is why I do: colorder = zip(*roworder) - (create a nested list column wise rather than row wise) and want to create a square order list - which is what the above mess does. I hope this clarifies things. Thanks again Colin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Pythonic nested lists
Hi all, I've been trying to write a programme that solves sudoku problems for a while now. I'm getting close, but would like to ask a few questions about the most pythonic way of doing some things. I've decided to create nested lists (row order, column order and square order) which correspond with dictionary keys - the values of which are the numbers given in the puzzle - so I can loop through the dict in different orders. I think the first two are OK, but the square order list seems extremely messy and I would love some pointers. What I have is as follows: roworder = [[str(j)+str(i) for i in range(9)] for j in range(9)] colorder = zip(*roworder) #here comes the problem! start, index = 0,0 sqorder = [] for i in range(9): sqorder.append([]) for j in range(start, start+3): sqorder[i].extend(roworder[j][index:index+3]) if index == 6: index = 0 else: index += 3 if i == 2 or i == 5: start += 3 Any comments at all would be gratefully received. Thanks in advance Colin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor