簡體   English   中英

我陷入Python錯誤TypeError:無法散列的類型:“切片”

[英]I got stuck on Python error TypeError: unhashable type: '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'

有任何想法嗎?

您正在將“列表”設置為字典的默認值: list = {} in:

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

然后使用以下命令將其分配給myList

self.myList = list

字典不能像列表一樣切成薄片。 因此,當您嘗試切片時:

self.myList[0:3]

它失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM