Python列表递归类型错误

Python列表递归类型错误,python,recursion,binary-tree,Python,Recursion,Binary Tree,已解决:问题似乎只发生在蟒蛇身上。我通过IDLE的pythonshell运行了所有程序,它运行得很好。必须是PythonWin的bug,而不是代码本身 我似乎不明白为什么下面的代码会给我一个TypeError:“type”对象是不可编辑的 巴斯德宾: 添加子节点的代码工作正常 def addChild(self, child): #add a child node. working if child.Value < self.Value: if self.l

已解决:问题似乎只发生在蟒蛇身上。我通过IDLE的pythonshell运行了所有程序,它运行得很好。必须是PythonWin的bug,而不是代码本身

我似乎不明白为什么下面的代码会给我一个TypeError:“type”对象是不可编辑的

巴斯德宾:

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

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)
任何帮助都将不胜感激

完整口译员会话: >>>作为BTN导入BinTreeNode >>>node1=BTN.BinaryTreeNode5 >>>node2=BTN.BinaryTreeNode2 >>>node3=BTN.BinaryTreeNode12 >>>node3=BTN.BinaryTreeNode16 >>>node4=BTN.BinaryTreeNode4 >>>node5=BTN.BinaryTreeNode13 >>>node1.addChildnode2 >>>node1.addChildnode3 >>>node1.addChildnode4 >>>node1.addChildnode5 >>>node4.genList >>>node1.genList 回溯最近一次呼叫上次: 文件,第1行,在 文件C:…\python\BinTreeNode.py,第47行,在genList中 numList.extendself.leftChild.genList错误 文件C:…\python\BinTreeNode.py,第52行,在genList中
TypeError:“type”对象不可编辑

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

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],而不是listself.Value。我有一种感觉它会起作用-

在您的示例中没有任何东西可以指明问题的来源,但它的意思是,您返回的是对象类型,而不是对象实例。在这一点上,我所能提供的只是建议重新编写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代替字符串

我建议对您的版本进行的编辑如下:

下面是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]

你能把整个错误都贴出来吗?或更多代码。leftChild和rightChild是如何生成的?@JacobJCallahan不要在评论中发布信息,因为在评论中你无法正确格式化信息。编辑您的问题。从您提供的代码来看,numList.extend似乎总是接收列表实例。我有一种感觉,这只能通过查看所有代码来解决:-/numList.extendlistself.Value,看起来应该是numList.appendself.Value。。。这不是你的问题,但它不会那么让人困惑——你没有碰巧在内置列表后面,对吧?@jgritty:不,那是错的。这将附加一个嵌套列表,而不是OP想要的。我通过解释器运行了这些命令,得到的结果是,出于某种原因,它们只是返回了一个列表类型@jacobjcalahan:是列表中的第一个打印元素,或者这是第二个元素?@jdi打印的内容是在没有子节点的节点上运行genList的结果。所以问题似乎是它返回的是类型而不是列表本身,[self.Value]返回的是same@JacobJCallahan当前位置我们需要了解更多的上下文以了解这里的错误。在您当前的示例中,没有任何内容指示错误的位置。我建议对您的pastebin进行编辑:尽管它在python2.7.3和Python3.3中对我来说很好。你能粘贴你的翻译结果吗?您可以使用与我完全相同的电话。谢谢您的帮助@jdi
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]