python将变量视为两种类型,但不能使用这两种类型中的任何一种方法

2024-10-02 12:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为8个谜题做BFS我将节点的状态保存在队列中并附加一个谜题节点类型,但是当我弹出节点并将其保存在变量中并尝试调用谜题节点方法时,我得到了这个错误AttributeError:“list”对象没有属性“state”,所以我将节点用作列表并调用了list方法,我得到了AttributeError:'Puzzle_Node'对象没有属性'sort',所以是什么导致了这个问题它以前使用几乎相同的代码,但现在我不知道是什么导致它更改类型

from collections import deque

class Puzzle_Node:


    def __init__(self, id, name, status, cost, state):
       self.nodeCount = 0
       self.name = name
       self.status = status
       self.cost = cost
       self.state = state
       self.id = id
       self.root = None
       self.leftChild = None
       self.rightChild = None

    def findZero(self, state):
      for row in range(len(state)):
          for column in range(len(state)):
             if state[row][column] == 0:
                 return row, column

    def swap(self, state, x1, y1, x2, y2):
        temp_puz = state
        temp = temp_puz[x2][y2]
        temp_puz[x2][y2] = temp_puz[x1][y1]
        temp_puz[x1][y1] = temp
        return temp_puz

    def check_goal(self, state):
       if self:
          for row in range(len(self.state)):
             for column in range(len(self.state)):
                 if self.state[row][column] == state[row][column]:
                     continue
                 else:
                     return False
         return True

    def makeCopyList(self, state, newList):
        newList = [
           [0, 0, 0],
           [0, 0, 0],
           [0, 0, 0],
          ]
        for row in range(len(state)):
           for column in range((len(state))):
              newList[row][column] = state[row][column]

        return newList

    def up(self, state, x, y):
        childState = self.swap(state, x, y, x - 1, y)
        return childState

    def down(self, state, x, y):
        childState = self.swap(state, x, y, x + 1, y)
        return childState

    def right(self, state, x, y):
        childState = self.swap(state, 2, 1, 2, 2)
        return childState

    def left(self, state, x, y):
        childState = self.swap(state, x, y, x, y - 1)
        return childState

    def generate_children(self, state):
        (x, y) = self.findZero(state)
        children = []

        stateList1 = []
        stateList1 = self.makeCopyList(state, stateList1)

        stateList2 = []
        stateList2 = self.makeCopyList(state, stateList2)

        stateList3 = []
        stateList3 = self.makeCopyList(state, stateList3)

        stateList4 = []
        stateList4 = self.makeCopyList(state, stateList4)

        stateStorage = [stateList1, stateList2, stateList3, stateList4]

        if x + 1 < 3:
           children.append(self.down(stateStorage[0], x, y))

        if x - 1 > -1:
           children.append(self.up(stateStorage[1], x, y))

         if y - 1 > -1:
            children.append(self.left(stateStorage[2], x, y))

         if y + 1 < 3:
            children.append(self.right(stateStorage[3], x, y))

         return children

    def BFS(self, goal):
        queue = deque()
        visited = []
        if self:
           queue.append(self)
           while not len(queue) <= 0:
               node = queue.popleft()
               if node.state is not None:
                  if node.state not in visited:
                    if node.check_goal(goal):
                          return node.state, visited.__str__()
                          visited.append(node.state)
                    newChildren = node.generate_children(node.state)
                    for i in range(len(newChildren)):
                         queue.append(newChildren[i]


state1 = [
    [4, 1, 3],
    [5, 7, 2],
    [8, 0, 6],
]

goalState = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 0],
]

node1 = Puzzle_Node(1, "k", 0, 1, state1)
(id, path) = node1.BFS(goalState)



   

Tags: inselfnodeforlenreturnifdef

热门问题