Python 8puzzle'__getitem__'错误

时间:2015-05-11 07:20:39

标签: python matrix

我正在尝试为我的学期项目创建一个8puzzle解算器。  当我尝试运行我的代码时,出现错误:

  

文件“8puzzle.py”,第234行,中      print best_first_search(initial_state)
   在best_first_search中文件“8puzzle.py”,第199行      explored_set = matrix_generator(m_1D)
   在matrix_generator中文件“8puzzle.py”,第181行      m_1D = convert_to_1D(矩阵)
   在convert_to_1D中输入第11行的文件“8puzzle.py”      matrix_1D.append(matrix_2D [行] [山口])
  TypeError:'int'对象没有属性' getitem '

我无法理解这个问题。

所以这是我的代码:

 import math,sys

goal_state = [[1,2,3],[4,5,6],[7,8,0]]   
initial_state = [[1,0,3],[4,2,5],[7,8,6]]   
initial_state_2 = [[0,2,3],[1,4,6],[7,8,5]]   

def convert_to_1D(matrix_2D):   
    matrix_1D = list()   
    for row in range(len(matrix_2D)):   
        for col in range(len(matrix_2D)):   
            matrix_1D.append(matrix_2D[row][col])   
    return matrix_1D   

def swap_blank_tile(matrix,next_pos):   
    pos=find_row_col(matrix,0)    
    blank_tile=find_row_col(matrix,0)   
    next_tile=find_row_col(matrix,next_pos)   
    set_row_col(matrix,pos,next_tile)    
    set_row_col(matrix,next_pos,blank_tile)   
    return matrix   

def hammingDistance(matrix):   
    array1=convert_to_1D(matrix)   
    arrayGoalState=convert_to_1D(goal_state)   
    tmp=0   
    i=0

    while(i<9):
        if((array1[i]!=arrayGoalState[i]) & (array1[i]!=0)):
            tmp=tmp+1
        i=i+1    
    return tmp


def manhattanDistance(matrix):   
    i=1    
    tmp=0    
    distinction=0

    while(i<9):
        row,col=find_row_col(matrix,i)
        row2,col2=find_row_col(goal_state,i)
        tmp=row+col
        tmp2=row2+col2
        distinction+=(math.fabs(tmp-tmp2))
        i=i+1
    return distinction


def swap(matrix, i, j):   
    matrix[i[0],i[1]], matrix[j[0],[1]] = matrix[j[0],j[1]], matrix[i[0],i[1]]    
    return matrix


def matrix_generator(matrix):    
    m_1D = convert_to_1D(matrix)    
    x = possible_moves(matrix)    
    list_of_states = []

    for i in x:
        new_current = []
        old_value = m_1D[i]
        new_current += swap(m_1D, m_1D.index(0), i)
        list_of_states.append(new_current)
        zero_index = m_1D.index(0)
        swap(matrix, m_1D.index(old_value), zero_index)
    return list_of_states


def best_first_search(matrix):   
    m_1D = convert_to_1D(matrix)   
    print m_1D  
    temp_solution = [m_1D]   
    explored_set = matrix_generator(m_1D)   
    step = 0   
    while goal_state:   
        step += 1

        for i in explored_set:
            explored_set += matrix_generator(i)
            x = hammingDistance(explored_set).pop()

            if x == goal_state:
                print "\nSolved in {} step(s)\n".format(step)
                return x

            elif x not in temp_solution:
                temp_solution += [x]
                explored_set = matrix_generator(x)
            else:
                explored_set.pop(explored_set.index(x))

            print x

print best_first_search(initial_state)

1 个答案:

答案 0 :(得分:2)

当我运行您的代码时,这就是发生的错误:

Traceback (most recent call last):
  File "solver.py", line 208, in <module>
    print best_first_search(initial_state)
  File "solver.py", line 187, in best_first_search
    explored_set = matrix_generator(m_1D)
  File "solver.py", line 169, in matrix_generator
    m_1D = convert_to_1D(matrix)
  File "solver.py", line 11, in convert_to_1D
    matrix_1D.append(matrix_2D[row][col])
TypeError: 'int' object has no attribute '__getitem__'

在最后一行中,这意味着当代码期待matrix_2D[row]int正在评估list。因此错误,因为int没有可以通过索引访问的元素!

您的函数matrix_generator期待2D矩阵,但在第187行中,您将其传递给1D矩阵。所以用m_1D替换matrix并从那里继续调试(因为一旦修复了这个代码就引发了一个不同的错误)。

我强烈建议您首先测试每个函数并确保它们按预期工作,然后对传递给每个函数的数据要更加小心。

编辑:行号可能不同,但我相信你可以找到相应的行:)