[英]Python list recursion type error
已解決:看來問題只發生在PythonWin。 我通過IDLE的python shell運行了所有程序,一切正常。 必須是PythonWin的錯誤,而不是代碼本身。
我似乎無法弄清楚為什么下面的代碼給我一個TypeError: 'type' object is not iterable
pastebin: 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
>>>節點1 = 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()
<class'list'>
>>> node1.genList()
追溯(最近一次通話):
<模塊>中第1行的文件“ <交互式輸入>”
genList中的文件“ C:... \\ python \\ BinTreeNode.py”,第47行
numList.extend(self.leftChild.genList())#錯誤
genList中的文件“ C:... \\ python \\ BinTreeNode.py”,第52行
TypeError:“類型”對象不可迭代
我會添加一些打印件,以查看出現錯誤時的實際類型,例如:
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
一個不太瘋狂的猜測...嘗試使用list((self.Value,))來使用[self.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'
而不是“ 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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.