在Python中遍历图形时,我收到此错误:
'dict'对象没有属性'has_key'
这是我的代码:
def find_path(graph, start, end, path=[]):
path = path + [start]
if start == end:
return path
if not graph.has_key(start):
return None
for node in graph[start]:
if node not in path:
newpath = find_path(graph, node, end, path)
if newpath: return newpath
return None
Run Code Online (Sandbox Code Playgroud)
该代码旨在找到从一个节点到另一个节点的路径.代码来源:http://cs.mwsu.edu/~terry/courses/4883/lectures/graphs.html
为什么我会收到此错误,如何解决?