0

I'm trying to list possibilities in a sudoku grid if another part of the program cannot solve and for some reason keep getting

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

even though the item does not use ands and the item in question is in fact asking is a single number != 0

The board pulls up a grid of 9x9 numbers and makes a solvability field for each one relying on boolean to see if there is only one solution.

def solve_sudoku(board):
    pos = ones((9,9,9), dtype= bool)
        for col in range (9):
            for row in range(9):
                   # iteration += 1
                val = board[row, col]
                if val != 0:
                    pos[row, col, :] = False
                    pos[row, :, val-1] = False
                    pos[:,col,val-1] = False
                    pos[((row/3)*3):((row/3)*3+3),((col/3)*3):((col/3)*3+3),val-1] = False
        for col in range(9):
            for row in range(9):
                if sum(pos[row,col,:]) == 1:
                    board[row, col] = list(pos[row, col, :]).index(True)+1

        for y in range(10):
            plot([-.05,9.05],[y,y],color='black', linewidth=1)
        for y in range(0, 10, 3):
            plot([-.05,9.05],[y,y],color='black', linewidth=3)

        for x in range(10):
            plot([x,x],[-.05,9.05],color='black', linewidth=1)
        for x in range(0, 10, 3):
            plot([x,x],[-.05,9.05],color='black', linewidth=3)
            axis('image')        
            axis('off')
        for x in range(9):
            for y in range(9):
                number = board[y,x]
                if number != 0:
                    plot(x+.5,9-(y+.5),marker='$'+str(number)+'$',markersize=15,color='black')
                else:
                    xpos = 0
                    for x in list(pos[row,col:]):
                        xpos += 1
                        xlist = []
                        if x.all() == True:
                            xlist.append[xpos]
                            plot(x+.5,9-(y+.5),xlist,markersize=5,color='purple')

And error

30         for y in range(9):
     31             number = board[y,x]
---> 32             if number != False:
     33                 plot(x+.5,9-(y+.5),marker='$'+str(number)+'$',markersize=15,color='black')
     34             else:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
4

1 回答 1

0

显然,board[y,x]不包含单个数字,而是一个数组

于 2013-10-15T21:44:58.080 回答