值错误:要取消固定的值太多

2024-05-04 22:14:17 发布

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

当我执行它时,它给了我一个eror,即太多的值,无法解包? 我怎样才能使它正常工作。

  stack = util.Stack()
  closed = []
  child = []
  index = 0
  currNode = problem.getStartState()
  node = currNode
  stack.push(node)
  while not stack.isEmpty():
     node = stack.pop()
     if problem.isGoalState(node):
        print "true"
        closed.append(node)
     else:
         child = problem.getSuccessors(node)
         for nodes in child:
            stack.push(nodes)
         closed.append(node)
  return None      

错误是:

 File  line 90, in depthFirstSearch
    child = problem.getSuccessors(node)
  File  line 179, in getSuccessors
    x,y = state
**ValueError: too many values to unpack**

getsuccession函数的代码是:

def getSuccessors(self, state):
    """
    Returns successor states, the actions they require, and a cost of 1.

     """

    successors = []
    for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
      x,y = state
      dx, dy = Actions.directionToVector(action)
      nextx, nexty = int(x + dx), int(y + dy)
      if not self.walls[nextx][nexty]:
        nextState = (nextx, nexty)
        cost = self.costFn(nextState)
        successors.append( ( nextState, action, cost) )

最初为此函数返回的值:

problem.getStartState() - (5, 5)
problem.isGoalState(problem.getStartState())- False
 problem.getSuccessors(problem.getStartState()) - [((5, 4), 'South', 1), ((4, 5), 'West', 1)]

Tags: inselfnodechildstackactionstateclosed
2条回答

不知道为什么传递给getSuccessors的元组大小不一致,但您可能可以通过检查node = stack.pop()行之后node的长度来修复它。如果是3,那么您需要在child = problem.getSuccessors(node)行中传递node[0]

首先,不太可能是整个getSuccessors方法,因为没有返回值。

为了猜测,我会说getSuccessors返回一个元组列表:(nextState,action,cost)。您将其中的每一个存储为节点,当您将其中一个传递回方法时,它将失败,并尝试将这三个值解包为两个。

你应该找到一个合适的调试器,并学习如何使用它。我使用Eclipse(与PyDev一起使用),它将显著地帮助您处理这些类型的错误。

相关问题 更多 >