我遇到了Python错误TypeError:unhashable类型:' slice'

时间:2015-10-01 06:14:19

标签: python hash slice

from informedSearch import *
from search import *

class EightPuzzleProblem(InformedProblemState):
    """
    Inherited from the InformedProblemState class. To solve
    the eight puzzle problem.
    """
    def __init__(self, myList, list = {}, operator = None):        
        self.myList = list
        self.operator = operator
    def __str__(self):

       ## Method returns a string representation of the state.  

        result = ""
        if self.operator != None:
            result += "Operator: " + self.operator + ""
        result += " " + ' '.join(self.myList[0:3]) + "\n"
        result += " " + ' '.join(self.myList[3:6]) + "\n"
        result += " " + ' '.join(self.myList[6:9]) + "\n"
        return result
    def illegal(self):
        ## Tests whether the state is illegal.
        if self.myList < 0 or self.myList > 9: return 1
        return 0

    def equals(self, state):

        ## Method to determine whether the state instance
        ## and the given state are equal.

        return ' '.join(self.myList) == ' '.join(state.myList)

    ## The five methods below perform the tree traversing
    def move(self, value):
        nList = self.myList[:] # make copy of the current state
        position = nList.index('P') # P acts as the key
        val = nList.pop(position + value) 
        nList.insert(position + value, 'P')
        nList.pop(position)
        nList.insert(position, val)
        return nList

    def moveleft(self):
        n = self.move(-1)
        return EightPuzzleProblem(n, "moveleft")

    def moveright(self):
        n = self.move(1)
        return EightPuzzleProblem(n, "moveright")

    def moveup(self):
        n = self.move(-3)
        return EightPuzzleProblem(n, "moveup")

    def movedown(self):
        n = self.move(+3)
        return EightPuzzleProblem(n, "movedown")

    def operatorNames(self):
        return ["moveleft", "moveright", "moveup", "movedown"]

    def enqueue(self):
        q = []
        if (self.myList.index('P') != 0) and (self.myList.index('P') != 3) and (self.myList.index('P') != 6):
            q.append(self.moveleft())
        if (self.myList.index('P') != 2) and (self.myList.index('P') != 5) and (self.myList.index('P') != 8):
            q.append(self.moveright())
        if self.myList.index('P') >= 3:
            q.append(self.moveup())
        if self.myList.index('P') >= 5:
            q.append(self.movedown())

    def applyOperators(self):
        return [self.moveleft(), self.moveright(), self.moveup(), self.movedown()]

    def heuristic():
        counter = 0
        for i in range(len(self.myList)):
            if ((self.myList[i] != goal.myList[i]) and self.myList[i] != 'P'):
                ## Position of current:
                current = goal.myList.index(self.myList[i])

            if current < 3: goalRow = 0
            elif current < 6: goalRow = 1
            else: goalRow = 2
            if i < 3: initRow = 0
            elif i < 6: initRow = 1
            else: startRow = 2

            initColumn = i % 3
            goalColumn = current % 3
            counter += (abs(goalColumn - initColumn) + abs(goalRow - initRow))
            return counter

#Uncomment to test the starting states:
init = ['1','3','P','8','2','4','7','6','5'] #A
#init = ['1','3','4','8','6','2','P','7','5'] #B
#init = ['P','1','3','4','2','5','8','7','6'] #C
#init = ['7','1','2','8','P','3','6','5','4'] #D
#init = ['8','1','2','7','P','4','6','5','3'] #E
#init = ['2','6','3','4','P','5','1','8','7'] #F
#init = ['7','3','4','6','1','5','8','P','2'] #G
#init = ['7','4','5','6','P','3','8','1','2'] #H
goal = ['1','2','3','8','P','4','7','6','5'] #goal state

InformedSearch(EightPuzzleProblem(init), EightPuzzleProblem(goal))                                    

我运行它并显示错误

line 34, in __str__ result += " " + ' '.join(self.myList[0:3]) + "\n"
TypeError: unhashable type: 'slice'

任何想法?

1 个答案:

答案 0 :(得分:0)

您将“列表”设置为字典作为默认值:list = {} in:

def __init__(self, myList, list = {}, operator = None):        

然后使用以下内容将其分配给myList

self.myList = list

字典不能像列表那样切片。所以当你尝试切片时:

self.myList[0:3]

失败了。