'dict'对象没有属性'has_key' [英] 'dict' object has no attribute 'has_key'

查看:243
本文介绍了'dict'对象没有属性'has_key'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中遍历图形时,我收到此错误:

While traversing a graph in Python, a I'm receiving this error:


'dict'对象没有属性'has_key'

'dict' object has no attribute '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

该代码旨在查找从一个节点到另一节点的路径。代码源: http://cs.mwsu.edu/~terry/ course / 4883 / lectures / graphs.html

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:



  • 删除了 dict.has_key() –使用 in 运算符。

  • Removed dict.has_key() – use the in operator instead.

下面是一个例子:

if start not in graph:
    return None

这篇关于'dict'对象没有属性'has_key'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆