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

标签 python python-3.x

当我尝试将旧的 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/

相关文章:

python - VS Code 的 Python 调试控制台中没有智能感知

python - 如何在某个 ID 的 Pandas 列中获得第二高的值?

python - 在 Python 字典中动态创建键和值

Python Twitter API Twitter错误: user_id must be type int

python - 有没有办法在其以前的版本中使用 Python 3.9 类型提示?

python - 为什么keras模型预测编译后会变慢?

python - 如何在 Python 3 中解码 HTTP 请求头和正文?

python - Pandas read_csv 中的复杂分隔列

python - 如何使用作为环境变量生成的 PyNaCl key

python - 在 scikit-image 中从文件系统加载自定义图像