gpt4 book ai didi

python - 尽管转换为列表,但获取 'dict_keys' 对象不支持索引

转载 作者:太空宇宙 更新时间:2023-11-04 06:54:42 24 4
gpt4 key购买 nike

我正在使用 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 中的哪个键,因此您可能希望自己对其进行排序。

关于python - 尽管转换为列表,但获取 'dict_keys' 对象不支持索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39002665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com