我在以下位置收到“dict_key object does not support indexing”错误消息:
return len(G[G.keys()[0]])
我意识到它曾经在 python 2.7.x 中工作但在 python 3 中不工作。我应该如何更改此语句以使其在 python 3 中工作。
最佳答案
在 Python 2.x 中,G.keys()
返回一个列表,但 Python 3.x 返回一个 dict_keys
对象。解决方案是通过调用 list()
来包装 G.keys()
,将其转换为正确的类型:
return len(G[list(G.keys())[0]])
关于python - dict_key 对象不支持索引-python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26693055/