首页 > 解决方案 > TypeError:'map' 对象在 python 3.8 中不可下标

问题描述

运行此程序时遇到问题:

file_Cond = open(filename2)
header_line = next(file_Cond)
tmp = map(int, header_line[8:].strip().split())
Nelements = tmp[0]
header_line = next(file_Cond)
tmp = map(int, header_line[8:].strip().split() )
Nnodes = tmp[0]
next(file_Cond)`

该行Nelements = tmp[0]引发以下错误:

TypeError: 'map' object is not subscriptable

你知道如何解决这个问题吗?

标签: python

解决方案


在 python 3.8 中,map函数返回一个地图对象而不是一个列表。您可以通过将其传递给 将其转换为列表list(..)

tmp = list(map(int, header_line[8:].strip().split()))


推荐阅读