python - 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 3.8 中,map
函数返回一个地图对象而不是一个列表。您可以通过将其传递给 将其转换为列表list(..)
。
tmp = list(map(int, header_line[8:].strip().split()))
推荐阅读
- c# - Linq通过select将匿名转换为实体对象
- python - 将 PostreSQL 枚举类型转换为 Django 客户字段
- database - DBMS_CRYPTO.ENCRYPT 在不同的 oracle DB 版本中返回不同的结果
- assembly - 为 IAR 装配调试 ARM Cortex-M 硬故障
- node.js - 使用 websocket 使用 Node.js 进行 Amazon Transcribe 流式传输
- excel - 从上个月的文件中获取一些数据到当月的报告中
- css - 如何使用 Material UI 应用我的 CRA 应用程序的 scss 样式?
- php - 如何将我的工作 Laravel 5.5 升级到 Laravel 6.0
- vba - CLICK 事件中的 VBA MSHFLEXGRID 错误 .ROW 信息
- r - 为什么列选择的 data.table 表示法会影响速度