Python “dict”对象没有属性“has_key”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33727149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:51:21  来源:igfitidea点击:

'dict' object has no attribute 'has_key'

pythonpython-3.xdictionary

提问by Ashi

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

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

'dict' object has no attribute 'has_key'

“dict”对象没有属性“has_key”

Here is my code:

这是我的代码:

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

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

Why am I getting this error and how can I fix it?

为什么我会收到此错误,我该如何解决?

回答by johnnyRose

has_keywas removed in Python 3. From the documentation:

has_key已在 Python 3 中删除。从文档中

  • Removed dict.has_key()– use the inoperator instead.
  • 已删除dict.has_key()- 改用in运算符。

Here's an example:

下面是一个例子:

if start not in graph:
    return None

回答by Kevin S

I think it is considered "more pythonic" to just use inwhen determining if a key already exists, as in

我认为仅in在确定密钥是否已存在时才使用它被认为是“更 Pythonic” ,如

if start not in graph:
    return None

回答by Oana Roxana

The whole code in the document will be:

文档中的整个代码将是:

graph = {'A': ['B', 'C'],
             'B': ['C', 'D'],
             'C': ['D'],
             'D': ['C'],
             'E': ['F'],
             'F': ['C']}
def find_path(graph, start, end, path=[]):
        path = path + [start]
        if start == end:
            return path
        if start not in graph:
            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

After writing it, save the document and press F 5

写完后,保存文档并按F 5

After that, the code you will run in the Python IDLE shell will be:

之后,您将在 Python IDLE shell 中运行的代码将是:

find_path(graph, 'A','D')

find_path(图形,'A','D')

The answer you should receive in IDLE is

您应该在 IDLE 中收到的答案是

['A', 'B', 'C', 'D'] 

回答by Abhishek Pansotra

has_keyhas been deprecated in Python 3.0. Alternatively you can use 'in'

has_keyPython 3.0 中已被弃用。或者,您可以使用“in”

graph={'A':['B','C'],
   'B':['C','D']}

print('A' in graph)
>> True

print('E' in graph)
>> False

回答by qloveshmily

In python3, has_key(key)is replaced by __contains__(key)

在python3中,has_key(key)被替换为__contains__(key)

Tested in python3.7:

在python3.7中测试:

a = {'a':1, 'b':2, 'c':3}
print(a.__contains__('a'))