【问题标题】:Python list recursion type errorPython列表递归类型错误
【发布时间】:2012-11-19 07:14:49
【问题描述】:

已解决:问题似乎只出现在 PythonWin 中。我通过 IDLE 的 python shell 运行了所有东西,它工作得很好。必须是 PythonWin 的错误,而不是代码本身。

我似乎无法弄清楚为什么下面的代码给了我一个TypeError: 'type' object is not iterable

粘贴箱:http://pastebin.com/VFZYY4v0

def genList(self):
    #recursively generates a sorted list of child node values
    numList = []
    if self.leftChild != 'none':
        numList.extend(self.leftChild.genList())  #error
    numList.extend(list((self.Value,)))
    if self.rightChild != 'none':
        numList.extend(self.rightChild.genList()) #error
    return numList

添加子节点的代码(正常工作)

def addChild(self, child):
    #add a child node. working
    if child.Value < self.Value:
        if self.leftChild == 'none':
            self.leftChild = child
            child.parent = self
        else:
            self.leftChild.addChild(child)
    elif child.Value > self.Value:
        if self.rightChild == 'none':
            self.rightChild = child
            child.parent = self
        else:
            self.rightChild.addChild(child)

任何帮助将不胜感激。

完整的口译会话: >>> 将 BinTreeNode 导入为 BTN
>>> node1 = BTN.BinaryTreeNode(5)
>>> node2 = BTN.BinaryTreeNode(2)
>>> node3 = BTN.BinaryTreeNode(12)
>>> node3 = BTN.BinaryTreeNode(16)
>>> node4 = BTN.BinaryTreeNode(4)
>>> node5 = BTN.BinaryTreeNode(13)
>>> node1.addChild(node2)
>>> node1.addChild(node3)
>>> node1.addChild(node4)
>>> node1.addChild(node5)
>>> node4.genList()

>>> node1.genList()
回溯(最近一次通话最后一次):

中的文件“”,第 1 行 文件“C:...\python\BinTreeNode.py”,第 47 行,在 genList
numList.extend(self.leftChild.genList()) #error
文件“C:...\python\BinTreeNode.py”,第 52 行,在 genList
TypeError:“类型”对象不可迭代

【问题讨论】:

  • 你能发布整个错误吗?或更多代码。 leftChild 和 rightChild 是如何生成的?
  • @JacobJCallahan 不要在 cmets 中发布信息,因为您无法正确格式化它。编辑您的问题。
  • 从您提供的代码来看,numList.extend() 似乎总是会收到一个列表实例。我觉得这只能通过查看所有代码来解决:-/
  • numList.extend(list((self.Value,))) 似乎应该只是numList.append(self.Value) ...(这不是你的问题,但它会不那么令人困惑)——你没有碰巧隐藏了内置的list是吗?
  • @jgritty:不,那是错误的。这将附加一个嵌套列表,这不是 OP 想要的。

标签: python recursion binary-tree


【解决方案1】:

我会添加一些打印来查看它给出错误时的实际类型,例如:

def genList(self):
    #recursively generates a sorted list of child node values
    numList = []
    if self.leftChild != 'none':
        print self.leftChild.genList(), type(self.leftChild.genList())
        numList.extend(self.leftChild.genList())  #error
    numList.extend(list((self.Value,)))
    if self.rightChild != 'none':
        print self.rightChild.genList(), type(self.rightChild.genList())
        numList.extend(self.rightChild.genList()) #error
    return numList

一个不那么疯狂的猜测...... 尝试使用 [self.Value] 代替 list((self.Value,))。我觉得它会起作用... :-)

【讨论】:

  • 我通过解释器运行命令并返回它们只是出于某种原因返回列表类型这是结果
  • @JacobJCallahan:第一个打印的元素是列表,还是第二个元素?
  • @jdi 打印的是在没有子节点的节点上运行 genList() 的结果。所以问题似乎是它返回的是类型而不是列表本身
  • 另外,[self.Value] 返回相同
  • @JacobJCallahan:我们需要查看更多上下文来了解这里出了什么问题。您当前的示例中没有任何内容表明错误的位置。
【解决方案2】:

您的示例中没有任何内容表明问题出在哪里,但这意味着您以某种方式返回了对象类型而不是对象实例。在这一点上,我所能提供的只是建议另一种方法来修改 genList() 方法,看看它是否能神奇地解决您的问题。

您可以尝试通过递归传递相同的结果列表,而不是返回大量临时结果:

def genList(self, numList=None):
    if numList is None:
        numList = []

    if self.leftChild != 'none':
        self.leftChild.genList(numList)

    numList.append(self.Value)

    if self.rightChild != 'none':
        self.rightChild.genList(numList)

    return numList

results = rootNode.genList()

另外,您使用 'none' 而不是 None 是否有原因?我只会使用None 而不是字符串。

我对您的版本的建议修改在这里:http://pastebin.com/FGf8Lcdu

这是您在 python3.3 下的相同解释器代码的输出:

In [1]: %cpaste
Pasting code; enter '--' alone on the line to stop or use Ctrl-D.
:>>> import BinTreeNode as BTN
:>>> node1 = BTN.BinaryTreeNode(5)
:>>> node2 = BTN.BinaryTreeNode(2)
:>>> node3 = BTN.BinaryTreeNode(12)
:>>> node3 = BTN.BinaryTreeNode(16)
:>>> node4 = BTN.BinaryTreeNode(4)
:>>> node5 = BTN.BinaryTreeNode(13)
:>>> node1.addChild(node2)
:>>> node1.addChild(node3)
:>>> node1.addChild(node4)
:>>> node1.addChild(node5)
:<EOF>

In [2]: node4.genList()
Out[2]: [4]

In [3]: node1.genList()
Out[3]: [2, 4, 5, 13, 16]

【讨论】:

  • 我建议对您的 pastebin 进行编辑:pastebin.com/FGf8Lcdu(尽管它在 python2.7.3 和 python 3.3 中对我来说效果很好)
  • 会的。你能粘贴你的解释器结果吗?您可以使用与我完全相同的调用。
  • 感谢您的所有帮助@jdi
猜你喜欢
  • 2016-12-29
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-14
  • 2011-08-17
  • 2012-11-09
  • 2022-12-14
相关资源
最近更新 更多