简体   繁体   中英

Python - If Statement with two-dimensional array

I'm trying to generate a Sudoku. I thought about using an array for this. THis is how I create the array (ignore the fact that these are not correct Sudoku numbers):

row_array = [[1, 99],[2],[3], [4], [5], [6], [7], [8], [9]]

It does work fine. The problem is with an if statement. If I do:

print(row_array[0][1])

or:

r = 1
print(row_array[0][r])

to get the "99" it does work fine! But if I use this variable method in an if statement like this:

if y not in row_array[1] and y not in row_array[0][r]:
    row_array[1].append(y)

I get the following ERROR :

TypeError: argument of type 'int' is not iterable

I had the Sudoku working, but it took to much code, so I want to set up a counter to replace the rows and columns with an it. I'm aware of you guys probably already having a much better code for a sudoku, but I want to teach myself, and make the code better and shorter over time. I don't learn to code real good, by just copy someone else's idea or way to code.

The problem is here:

if y not in row_array[0][r]

The in operator checks if the left-hand side is in the sequence or iterable on the right hand side. But type(row_array[0][r]) will return int , which is not an iterable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM