数独中的Numpy Truth Ambiguous Array

时间:2013-10-15 21:42:17

标签: python arrays numpy ambiguous

如果程序的另一部分无法解决并且由于某种原因不断解决,我正试图列出数独网格中的可能性

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

即使该项目没有使用ands,并且有问题的项目实际上是询问是一个数字!= 0

董事会提取一个9x9数字的网格,并为依赖于布尔值的每个数据生成一个可解决性字段,以查看是否只有一个解决方案。

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')

错误

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()

1 个答案:

答案 0 :(得分:0)

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