# [PyAR2] TypeError: 'int' object is not subscriptable

Daniel Veazey daniel at danielveazey.com
Mon Aug 22 22:43:39 CEST 2011

```Thanks for your comments and help. I have finished the first version of the
program, and here is a link to the code:

http://paste.ubuntu.com/672710/

It only uses the most basic of methods to solve puzzles, so even some
puzzles rated as "easy" still stump it. But it does what I set out for it to
do, and I'm proud of that. I plan to incorporate more advanced methods to
solve harder puzzles later. I learned a lot doing this. Thanks again for the
help. If you want to try it out, I'd appreciate any feedback.

Thanks,
Daniel

On Sun, Aug 21, 2011 at 1:54 PM, Carl Burch <dr.cburch at gmail.com> wrote:

> Yes, you access an element with r[i][j], since after all a 2d array is just
> the same as a list of lists; r[i] is a single one of those lists, and you'd
> add [j] to extract an element from that list.
>
> To go further beyond what you ask: It's also interesting how you can do the
> same thing with list comprehensions:
>
> col[0] = [r[0] for r in row]
>
> Here, r iterates through each row in the row array, and we build up an
> array of all the r[0]'s.
>
> You could repeat this same line 8 more times, but I'd put it into a loop:
>
> for i in range(9):
>   col[i] = [r[i] for r in row]
>
> Or we could even nest the comprehension into another comprehension:
>
> col = [[r[i] for r in row] for i in range(9)]
>
> Maybe somebody else has an even cleverer way to build up the translation of
> a 2d array.
>
> -Carl
>
> On Aug 21, 2011, at 12:20 PM, Coltrey Mather <pyar2 at cowsgomoo.org> wrote:
>
> On Aug 21, 2011 9:04 AM, "Daniel Veazey" < <daniel at danielveazey.com>
> daniel at danielveazey.com> wrote:
> > In my efforts to learn Python, I'm trying to write a program to solve a
> > Sudoku puzzle. Here is the code I have so far:
> >
> > ## define and populate the board by rows; 0 = unsolved position
> > row = [0, 1, 2, 3, 4, 5, 6, 7, 8]
> > row[0] = [5, 6, 0, 0, 4, 1, 0, 0, 8]
> > row[1] = [1, 0, 0, 0, 0, 9, 0, 7, 0]
> > row[2] = [0, 8, 0, 5, 0, 2, 3, 0, 0]
> > row[3] = [3, 0, 9, 0, 2, 0, 0, 5, 0]
> > row[4] = [0, 7, 0, 3, 0, 5, 0, 9, 0]
> > row[5] = [0, 5, 0, 0, 7, 0, 6, 0, 1]
> > row[6] = [0, 0, 2, 1, 0, 3, 0, 6, 0]
> > row[7] = [0, 3, 0, 2, 0, 0, 0, 0, 9]
> > row[8] = [6, 0, 0, 4, 5, 0, 0, 8, 3]
> >
> > ## define and populate the columns by referring to the rows
> > col = [0, 1, 2, 3, 4, 5, 6, 7, 8]
> > col[0] = [row[0[0]], row[1[0]], row[2[0]], row[3[0]], row[4[0]],
> row[5[0]],
> > row[6[0]], row[7[0]], row[8[0]]]
> > col[1] = [row[0[1]], row[1[1]], row[2[1]], row[3[1]], row[4[1]],
> row[5[1]],
> > row[6[1]], row[7[1]], row[8[1]]]
> > col[2] = [row[0[2]], row[1[2]], row[2[2]], row[3[2]], row[4[2]],
> row[5[2]],
> > row[6[2]], row[7[2]], row[8[2]]]
> > col[3] = [row[0[3]], row[1[3]], row[2[3]], row[3[3]], row[4[3]],
> row[5[3]],
> > row[6[3]], row[7[3]], row[8[3]]]
> > col[4] = [row[0[4]], row[1[4]], row[2[4]], row[3[4]], row[4[4]],
> row[5[4]],
> > row[6[4]], row[7[4]], row[8[4]]]
> > col[5] = [row[0[5]], row[1[5]], row[2[5]], row[3[5]], row[4[5]],
> row[5[5]],
> > row[6[5]], row[7[5]], row[8[5]]]
> > col[6] = [row[0[6]], row[1[6]], row[2[6]], row[3[6]], row[4[6]],
> row[5[6]],
> > row[6[6]], row[7[6]], row[8[6]]]
> > col[7] = [row[0[7]], row[1[7]], row[2[7]], row[3[7]], row[4[7]],
> row[5[7]],
> > row[6[7]], row[7[7]], row[8[7]]]
> > col[8] = [row[0[8]], row[1[8]], row[2[8]], row[3[8]], row[4[8]],
> row[5[8]],
> > row[6[8]], row[7[8]], row[8[8]]]
> >
> > When I run the program, the first part of defining the rows runs fine,
> but
> > when I start to define the columns, I get this error:
> >
> > Traceback (most recent call last):
> > File "sudoku_solver_1.py", line 15, in <module>
> > col[0] = [row[0[0]], row[1[0]], row[2[0]], row[3[0]], row[4[0]],
> > row[5[0]], row[6[0]], row[7[0]], row[8[0]]]
> > TypeError: 'int' object is not subscriptable
> >
> > I'm not quite sure what I'm doing wrong here. I want to nest a list
> inside
> > the list col by referring to the nested lists in row. Any tips on why I'm
> > getting this error or what I should do to fix it?
> >
> > Thanks,
> > Daniel
>
> _______________________________________________
> PyAR2 mailing list
> PyAR2 at python.org
> http://mail.python.org/mailman/listinfo/pyar2
>
>
-------------- next part --------------
An HTML attachment was scrubbed...