Question

so I am trying to create a Sudoku Board in Python and have the NN run and learn to solve it. But I am having issues trying to set these initial values to 0. Rather than having to write out a1=0, a2=0, etc. Is there a faster way to assign these values in a simple code? Thanks.

def create():
print '| - - - | - - - | - - - |',
print '|', a1, a2, a3, '|', b1, b2, b3, '|', c1, c2, c3, '|',
print '|', a4, a5, a6, '|', b4, b5, b6, '|', c4, c5, c6, '|',
print '|', a7, a8, a9, '|', b7, b8, b9, '|', c7, c8, c9, '|',
print '| - - - | - - - | - - - |',
print '|', d1, d2, d3, '|', e1, e2, e3, '|', f1, f2, f3, '|',
print '|', d4, d5, d6, '|', e4, e5, e6, '|', f4, f5, f6, '|',
print '|', d7, d8, d9, '|', e7, e8, e9, '|', f7, f8, f9, '|',
print '| - - - | - - - | - - - |',
print '|', g1, g2, g3, '|', h1, h2, h3, '|', i1, i2, i3, '|',
print '|', g4, g5, g6, '|', h4, h5, h6, '|', i4, i5, i6, '|',
print '|', g7, g8, g9, '|', h7, h8, h9, '|', i7, i8, i9, '|',
print '| - - - | - - - | - - - |',
Was it helpful?

Solution

You're probably going to want to use a list for this.

If you really want to keep it the way you're doing it, you can do this:

a1 = a2 = a3 ... = i9 = 0

or to save some writing space

#(0,)*81 is the same thing as writing 0, 0, ..., 0
a1, a2, a3, ..., i9 = (0,)*81 

but I suggest you use a list of lists.

Here's a list comprehension that creates a list of 9 zeros:

a = [0 for x in range(9)]

Use this idea to create a list comprehension that creates a list of 9 lists.

grid = [[0 for x in range(9)] for y in range(9)]

Then you can access grid like so:

>>> print grid[0][0]
>>> 0
>>> print grid[8][8]
>>> 0
>>> print grid
>>> [[0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0]]

OTHER TIPS

When working with such structures in Python I would definitly suggest to give a look at the numpy.matrix.

You can just write

>>> import numpy
>>> m = numpy.zeros(shape=(9,9))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top