我有一个用于从一组节点构建树的函数
def buildTree(self, nodeList,):
if len(nodeList) > 1:
nodeList = sorted(nodeList, key=self.getKey)
newNode = self.makeNode(nodeList[0], nodeList[1])
nodeList.pop(0)
nodeList.pop(0)
nodeList.append(newNode)
nodeList = self.buildTree(nodeList)
else:
print type(nodeList[0])
return nodeList[0]
此输出类型为“instance”
from TreeFactory import TreeFactory
nodeList = builder.buildNodeList(
'the quick brown fox jumped over the lazy dog')
nodeHead = builder.buildTree(nodeList)
print type(nodeHead)
当我打印生成器返回的类型时,我得到“type'NoneType'”打印出来
那么为什么它在返回之前是一个有效实例,而不是在返回之后呢
根据https://stackoverflow.com/users/367273/npe,问题是if分支没有返回任何内容,因此递归导致nodeList被None值填充。通过更改buildTree函数,使其
解决了这个问题
相关问题 更多 >
编程相关推荐