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

时间:2015-01-21 07:22:27

标签: python python-2.7

我正在编写一个脚本来解决数独游戏,我最初只为9x9构建它,现在我将它扩展到任何数独游戏。该脚本适用于4x4和9x9但是当我给它一个16x16时它会返回TypeError:类型'int'的参数在第38行不是可迭代的python 2.7,'如果我在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]])

提前致谢

1 个答案:

答案 0 :(得分:0)

你的问题来自这一行:

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

现在你正在做更大的谜题,一个两位数的数字会返回True,即使它只有一个值。

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

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