python - 类型错误:列表索引必须是整数,而不是元组,怎么了

标签 python syntax-error typeerror

<分区>

Python 中的新功能,帮助。 为什么我收到此错误: “TypeError:列表索引必须是整数,而不是元组,”

imheight = []
for i in range(0,len(tables)):
    for j in range(0,len(tables)):
      hij = computeHeight(imp[i],imp[j],'Meter')
      imheight[i,j] = hij
      imheight[j,i] = hij

最佳答案

语法错误:

imheight[i,j] = hij
imheight[j,i] = hij

也许你是这个意思?

imheight[i][j] = hij 
imheight[j][i] = hij

不过话又说回来,imheight 是一个一维列表,但您假设它是一个二维矩阵。它只有在您首先正确初始化 imheight 时才会起作用:

imheight = [[0] * len(tables) for _ in range(len(tables))]

关于python - 类型错误:列表索引必须是整数,而不是元组,怎么了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19780320/

相关文章:

python - 使用变量初始化解释器

Haskell 关系错误 - 声明中的语法错误(意外的 `;' ,可能是由于布局错误)

php - SQL 语法错误或服务器错误?

python - 使用Python从嵌套json字典获取信息的问题

javascript - 类型错误为空

python - Django 中的多个嵌套外键关系

python - 在 Python 中将二进制列表转为 PNG

javascript - 初学者 react : Render Syntax Error

javascript - 类型错误 : Undefined is not an object (evaluating this. props.navigation.navigate)

python - 在 Python 中还有哪些其他方法可以编写 if x==1 or x==5 or x==10...?