‘dict’对象没有属性’has_key’

问题:’dict’对象没有属性’has_key’

在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

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

为什么会出现此错误,我该如何解决?

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

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

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


回答 0

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

  • 已删除dict.has_key()–请改用in运算符。

这是一个例子:

if start not in graph:
    return None

has_key was removed in Python 3. From the documentation:

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

Here’s an example:

if start not in graph:
    return None

回答 1

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

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

print('A' in graph)
>> True

print('E' in graph)
>> False

has_key has been deprecated in Python 3.0. Alternatively you can use ‘in’

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

print('A' in graph)
>> True

print('E' in graph)
>> False

回答 2

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

在python3.7中测试:

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

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

Tested in python3.7:

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

回答 3

我认为,仅in在确定某个键是否已存在时才使用它,它被认为是“更多的pythonic” ,如

if start not in graph:
    return None

I think it is considered “more pythonic” to just use in when determining if a key already exists, as in

if start not in graph:
    return None

回答 4

该文档中的整个代码将为:

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

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

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

find_path(图,’A’,’D’)

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

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

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

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

find_path(graph, ‘A’,’D’)

The answer you should receive in IDLE is

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

回答 5

尝试:

if start not in graph:

有关更多信息,请参见ProgrammerSought

Try:

if start not in graph:

For more info see ProgrammerSought