Python TypeError: 'list' object is not callable with a dict dictionary, if boolean

Python TypeError: 'list' object is not callable with a dict dictionary, if boolean

我有一个字典,可以将索引号转换为节点名称。我正在尝试检查该名称是否已在列表中。我得到一个错误列表 'object not callable'。附上我的 code 的图像。

visited() 更改为 visited...

看看这一行,

if index2node[index] not in visited()

从下面的行来看,您似乎将 visited 视为可调用对象,即尝试调用它。

但是看看这行,

visited = list()

因此,visited 是一个列表,不是可调用对象。它没有 __call__ 方法。要检查这一点,请执行:

print hasattr(visited, '__call__')

在您的情况下,因为您只想检查列表中的一个元素,请执行以下操作:

if index2node[index] not in visited: