本文介绍了'dict'对象没有属性'has_key'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Python中遍历图形时,我收到此错误:
While traversing a graph in Python, a I'm receiving this error:
这是我的代码:
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
该代码旨在查找从一个节点到另一节点的路径。代码源:
The code aims to find the paths from one node to others. Code source: http://cs.mwsu.edu/~terry/courses/4883/lectures/graphs.html
为什么会出现此错误,如何解决?
Why am I getting this error and how can I fix it?
推荐答案
has_key
在Python 3中已删除。从:
has_key
was removed in Python 3. From the documentation:
下面是一个例子:
if start not in graph:
return None
这篇关于'dict'对象没有属性'has_key'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!