gpt4 book ai didi

python - 属性错误 : 'map' obejct has no attribute 'index' (python 3)

转载 作者:太空狗 更新时间:2023-10-30 00:39:30 24 4
gpt4 key购买 nike

当我尝试将旧的 python 代码库运行到 python3 时,我遇到了上述运行时错误。代码如下所示。

index = map(lambda x: x[0], self.history).index(state)

最佳答案

在 Python 3 中,map 不返回 list,而是返回 map object - 参见:

index = map(lambda x: x[0], [(1,2),(3,4)])
print( type(index) )
# <class 'map'>

你必须使用 list()

index = list(map(lambda x: x[0], self.history)).index(state)

关于python - 属性错误 : 'map' obejct has no attribute 'index' (python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33717314/

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