问题1
来源:在python中使用Matplotlib注解绘制树形图
描述:getNumLeafs(myTree)和getTreeDepth(muTree)中,firstStr = myTree.keys()[0]等带有myTree.keys()的地方都编译通不过
报错:TypeError: 'dict_keys' object does not support indexing
解决办法:将函数中的firstStr = myTree.keys()[0]改成firstStr = list(myTree.keys())[0]
原因:书中所用代码为python2,而我使用的是python3,python3中dict.keys有改动,返回的是dict_keys对象,支持iterable,但不支持indexable,将其明确的转化成list即可运行
问题2
来源:在python中使用Matplotlib注解绘制树形图
描述:getTreeDepth(muTree)中,将firstStr = myTree.keys()[0]改成firstStr = list(myTree.keys())[0]后还是编译不过
报错:[Previous line repeated 994 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object
解决办法:插入代码
import sys
sys.setrecursionlimit(10000)
将递归深度调整到一万层
原因:循环代码执行到994 次的时候出现‘RecursionError: max