我正在使用 Python 3,尽管转换为列表,但我似乎无法运行我的程序。
这是函数调用:
path = euleriancycle(edges)
这就是我使用 keys 方法的地方:
def euleriancycle(e):
currentnode = list[e.keys()[0]]
path = [currentnode]
我试图在没有类型转换的情况下运行它以列出并得到这个错误。在翻遍了这个网站和类似的查询之后,我按照建议的解决方案进行了类型转换以列出但无济于事。我得到了同样的错误。
这是错误轨迹:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-56-356b905111a9> in <module>()
45 edges[int(edge[0])] = [int(edge[1])]
46
---> 47 path = euleriancycle(edges)
48 print(path)
<ipython-input-56-356b905111a9> in euleriancycle(e)
1 def euleriancycle(e):
----> 2 currentnode = list[e.keys()[0]]
3 path = [currentnode]
4
5 while true:
TypeError: 'dict_keys' object does not support indexing
请您参考如下方法:
dict_keys 对象,如集合,不能被索引。
取而代之的是:
list[e.keys()[0]]
下一个最接近的是:
list(e)[0]
Python 不保证将返回 dict 中的哪个键,因此您可能希望自己对其进行排序。
