简体   繁体   English

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

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

I'm writing a script to solve sudoku puzzles, I originally built it just for 9x9, and am now expanding it to any sudoku. 我正在写一个脚本来解决数独难题,我最初只是为9x9构建的,现在又将其扩展到任何数独。 The script works on 4x4 and 9x9 but when I give it a 16x16 it returns TypeError: argument of type 'int' is not iterable python 2.7 at line 38, 'if i in dic[entry]: dic[entry].remove(i)' 该脚本可以在4x4和9x9上运行,但是当我给它一个16x16时,它返回TypeError:类型'int'的参数在第38行的Python 2.7中不是可迭代的,'if i in dic [entry]:dic [entry] .remove(i )”

heres my code 这是我的代码

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

and heres two test puzzles 这是两个测试难题

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

Thanks in advance 提前致谢

Your problem is from this line: 您的问题来自此行:

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

Now that you're doing larger puzzles, a double-digit number will return True , even though it only has a single value. 现在您正在做更大的难题,即使它只有一个值,一个两位数的数字也将返回True

It's not clear to me why you need to convert all these things to strings. 我不清楚,为什么需要将所有这些都转换为字符串。 It seems to me you can store them as lists and just check if the length of the list is greater than 1. 在我看来,您可以将它们存储为列表,然后检查列表的长度是否大于1。

Otherwise, you can test whether type is list or int. 否则,您可以测试type是list还是int。

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

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