类型错误:列表索引必须是整数,而不是元组,出了什么问题 [英] TypeError: list indices must be integers, not tuple, whats wrong

查看:33
本文介绍了类型错误:列表索引必须是整数,而不是元组,出了什么问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 中的新功能,帮助.为什么我收到这个错误:类型错误:列表索引必须是整数,而不是元组,"

New in Python, help. Why i get this error: "TypeError: list indices must be integers, not tuple,"

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 时才有效:

But then again, imheight is a one-dimensional list, but you're assuming that it's a two-dimensional matrix. It will only work if you first initialize imheight correctly:

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

这篇关于类型错误:列表索引必须是整数,而不是元组,出了什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆