python - 值错误 : Too many values to unpack

标签 python

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

  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**

getsuccessor 函数的代码是:

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)]

最佳答案

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

猜测一下,我想说 getSuccessors 返回一个元组列表:(nextState、action、cost)。您将每个值存储为节点,当您将一个值传递回该方法时,该方法将会失败,并且它会尝试将三个值解压为两个值。

你应该找到一个像样的调试器,并学习如何使用它。我用Eclipse (使用 PyDev ),它将极大地帮助您解决这些类型的错误。

关于python - 值错误 : Too many values to unpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3251122/

相关文章:

python - 基于索引的 2 列 2 数据帧之间的差异

python - 计算排列列表中的周期

python - ' ^' Character Matches Both ' <' and ' >' 在 Python 正则表达式中

python - 遍历二维 python 列表

python - 使用 Rasterio 和 Fiona 剪切栅格

python - 在 Python 中检测 NUMLOCK/CAPSLOCK/SCRLOCK keypress/keyup

python - 右键单击QTableWidget获取标题列

python - sublime区分单行注释和多行注释

python - Django DATETIME_FORMAT 特定日期不正确(一年)

java - 如何在运行时从 python 提供 Java 输入?