Python 类型错误:列表索引必须是整数,而不是列表。怎么修?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22431647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 00:56:07  来源:igfitidea点击:

TypeError: list indices must be integers, not list. How to fix?

pythonlisttypeerrorindices

提问by Larleyt

There's the code with the TypeError in it. "list indices must be integers, not list", though they are integers. I'd appreciate you helping me figure out what's wrong. What I need is to get matrix 7x5 from source 7x5 tab with different values. The error occurs on the last line.

有包含 TypeError 的代码。“列表索引必须是整数,而不是列表”,尽管它们是整数。我很感激你帮我弄清楚出了什么问题。我需要的是从具有不同值的源 7x5 选项卡中获取矩阵 7x5。错误发生在最后一行。

lines = []

with open("text.txt") as f:
    for line in f:
        line = [int(x) for x in line if (x != ' ') and (x != '\n')]
        lines.append(line)
    f.close()

What I have after reading file is list of lists with numbers called "lines". It's integers. Not strings. Like:

阅读文件后我得到的是带有数字的列表列表,称为“行”。是整数。不是字符串。喜欢:

>> [[1, 2, 3...], [4, 5, 6...], [7, 8, 9...],[...]]


i = 1
j = 1
T = []
T.append(lines[0][0]) 

I made this for avoiding IndexError(list out of range) on last line (i-1and stuff). Though, I don't think it's python-way really. I'd appreciate help with this thing too.

我这样做是为了避免IndexError在最后一行(i-1和其他内容)上(列表超出范围)。虽然,我不认为它真的是 python 方式。我也很感激这件事的帮助。

for i in lines:
    for j in lines:
        T[i][j] = lines[i][j] + max(T[i][j-1], T[i-1][j])

This is where error occurs. I don't get what should I fix if i, jare already int.

这是发生错误的地方。如果i, jare already ,我不知道我应该修复什么int

回答by user2357112 supports Monica

for i in lines:
    for j in lines:

iand jiterate over the elements of lines, not the indices. That means iand jare always lists, entire lines of numbers.

ij迭代 的元素lines,而不是索引。这意味着i并且j始终是列表,整行数字。

If you want to go over the indices (usually you don't, but it may be the best option here), you want

如果你想查看索引(通常你不想,但它可能是这里的最佳选择),你想要

for i in range(len(lines)):
    for j in range(len(lines[i])):

This is awkward by design, as the Python designers want people to default to iterating over the elements of a sequence.

这在设计上很尴尬,因为 Python 设计者希望人们默认迭代序列的元素。

Also, note that your loop tries to access elements of linesbefore the first row and before the first column. Perhaps you want to start your loops on the second row and column.

另请注意,您的循环尝试访问lines第一行之前和第一列之前的元素。也许您想在第二行和第二列开始循环。

回答by Martijn Pieters

iand jare not indices; they are values from the lineslist. Python forloops are for-each constructs.

i并且j不是指数;它们是lines列表中的值。Pythonfor循环是for-each 结构

Use:

用:

for i, line in enumerate(lines):
    for j, value in enumerate(line):
        T[i][j] = value + max(T[i][j - 1 % len(T[i])] + T[i - 1 % len(T)][j])

where the % len()calculations 'wrap around' to the last entry in Tor T[i]when iand / or jare 0. The enumerate()functionadds indices to the loop.

其中% len()计算“环绕”到最后一个条目 in Tor T[i]wheni和/或jare 0。该enumerate()函数向循环添加索引。

This does assume you already pre-built a nested list of lists structure in T.

这确实假设您已经在T.