繁体   English   中英

TypeError:类型'int'的参数不可迭代python 2.7

[英]TypeError: argument of type 'int' is not iterable python 2.7

我正在写一个脚本来解决数独难题,我最初只是为9x9构建的,现在又将其扩展到任何数独。 该脚本可以在4x4和9x9上运行,但是当我给它一个16x16时,它返回TypeError:类型'int'的参数在第38行的Python 2.7中不是可迭代的,'if i in dic [entry]:dic [entry] .remove(i )”

这是我的代码

from math import sqrt
def sudoku(puzzle):
    n = len(puzzle)
    # Builds Dictionary with board position and value
    dic = {str(i+1) + '-' + str(j+1): puzzle[i][j] for i in xrange(0, n) for j in xrange(0, n)}
    # Builds list of each possible value for all empty (0) spaces, only considers row and column possibilities
    while 0 in dic.values():
        for entry in dic:
            if dic[entry] == 0:
                temp1 = []
                temp2 = []
                for entry1 in dic:
                    if entry1[0] == entry[0]: temp1.append(dic[entry1])
                    if entry1[-1] == entry[-1]: temp1.append(dic[entry1])
                for i in xrange(1, n+1):
                    if i not in temp1: temp2.append(i)
                dic[entry] = temp2
    # populates dictionary with invalid spaces in each square
        sqrdic = {str(i)+str(j): [] for i in xrange(1, int(sqrt(n))+1) for j in xrange(1, int(sqrt(n))+1)}
        for entry in dic:
            if len(str(dic[entry])) == 1:
                for index in xrange(1, int(n/sqrt(n)+1)):
                    if (index-1)*sqrt(n) < int(entry[0]) <= index*sqrt(n):
                        for index2 in xrange(1, int(sqrt(n))+1):
                            if (index2-1)*sqrt(n) < int(entry[-1]) <= index2*sqrt(n):
                                sqrdic[str(index) + str(index2)].append(dic[entry])
        # Removes invalid choices based on values in the square
        for entry in dic:
            if len(str(dic[entry])) > 1:  # only looking at spaces with multiple possible values
                for index in xrange(1, int(n/sqrt(n)+1)):
                    if (index-1)*sqrt(n) < int(entry[0]) <= index*sqrt(n):
                        for index2 in xrange(1, int(sqrt(n))+1):
                            if (index2-1)*sqrt(n) < int(entry[-1]) <= index2*sqrt(n):
                                for i in sqrdic[str(index)+str(index2)]:
                                    if i in dic[entry]: dic[entry].remove(i)
        # Looks for any space whose possibilities have been reduced to one and replaces the list with that value
        # All unsolved spaces are then set back to 0
        for entry in dic:
            if type(dic[entry]) is list and len(dic[entry]) == 1:
                dic[entry] = int(dic[entry][0])
            elif type(dic[entry]) is list and len(dic[entry]) > 1: dic[entry] = 0
    solution = [[dic[str(j)+"-"+str(i)] for i in xrange(1,n+1)] for j in xrange(1,n+1)]
    for line in solution: print line

这是两个测试难题

sudoku([[1,0,0,2,3,4,0,0,12,0,6,0,0,0,7,0],
       [0,0,8,0,0,0,7,0,0,3,0,0,9,10,6,11],
       [0,12,0,0,10,0,0,1,0,13,0,11,0,0,14,0],
       [3,0,0,15,2,0,0,14,0,0,0,9,0,0,12,0],
       [13,0,0,0,8,0,0,10,0,12,2,0,1,15,0,0],
       [0,11,7,6,0,0,0,16,0,0,0,15,0,0,5,13],
       [0,0,0,10,0,5,15,0,0,4,0,8,0,0,11,0],
       [16,0,0,5,9,12,0,0,1,0,0,0,0,0,8,0],
       [0,2,0,0,0,0,0,13,0,0,12,5,8,0,0,3],
       [0,13,0,0,15,0,3,0,0,14,8,0,16,0,0,0],
       [5,8,0,0,1,0,0,0,2,0,0,0,13,9,15,0],
       [0,0,12,4,0,6,16,0,13,0,0,7,0,0,0,5],
       [0,3,0,0,12,0,0,0,6,0,0,4,11,0,0,16],
       [0,7,0,0,16,0,5,0,14,0,0,1,0,0,2,0],
       [11,1,15,9,0,0,13,0,0,2,0,0,0,14,0,0],
       [0,14,0,0,0,11,0,2,0,0,13,3,5,0,0,12]])

sudoku([[0,0,0,6,0,5,9,4,7],
      [4,7,2,8,0,9,6,1,5],
      [0,0,9,1,0,4,8,0,0],
      [1,2,4,0,5,8,0,0,0],
      [7,3,8,2,9,6,1,5,4],
      [0,0,0,3,4,1,2,7,8],
      [0,0,7,9,0,3,5,0,0],
      [5,9,1,4,0,2,7,8,3],
      [3,8,6,5,0,7,0,0,0]])

提前致谢

您的问题来自此行:

if len(str(dic[entry])) > 1:  # only looking at spaces with multiple possible values

现在您正在做更大的难题,即使它只有一个值,一个两位数的数字也将返回True

我不清楚,为什么需要将所有这些都转换为字符串。 在我看来,您可以将它们存储为列表,然后检查列表的长度是否大于1。

否则,您可以测试type是list还是int。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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