索引错误:列出超出范围的赋值索引——带有数组的Python

时间:2021-01-02 16:38:44

I recently started to use python and i am still newbie with many things of the language. This piece of code should print a serie rows (Ex.:[47.815, 47.54, 48.065, 57.45]) that i take as imput from several text files (called 2t1,...,2t19,3t1,...,3t19) and the name of the file after it's over. Every file has 80 elements that must be saved (for a total of ~12000 elements, ~80 rows for each file, 4 elements each row)

我最近开始使用python,而且我还是这门语言的新手。这段代码应该打印一个列(例如:[47.815,47.54,48.065,57.45]),我从几个文本文件(叫做2t1,……,2t19,3t1,……,3t19)中输入它们,然后是文件的名称。每个文件有80个必须保存的元素(总共有~12000个元素,每个文件有~80行,每行4个元素)

for i in range(2,3):
for j in range(1,19):
    #Open a text file and convert the row content to float:
    for line in open('[Path]/%dt%d.txt' % (i,j)):
    # Ignore comments:
        if not line.startswith('#'):
            # Got a valid data line:
                row[i][j] = [float(item) for item in line.split()]
                print (row[i][j])
    print (' %dt%d \n' %(i,j))

It is able to do what i ask it, but just until 2t5 and then it is shown this message:

它可以按照我的要求去做,但是直到2t5,然后它会显示如下信息:

Traceback (most recent call last):

回溯(最近一次通话):

File "< tmp 1>", line 8, in < module>

文件“< tmp 1>”,第8行,在< module>中

row[i][j] = [float(item) for item in line.split()]

行[i][j] = [float(项目),用于行。split()]

IndexError: list assignment index out of range

索引错误:列表分配索引超出范围

I can't really figure out what's the problem, i guess the list isn't filled to it's max (i read online that it has 500.000.000 spaces), and i can't see what may be wrong . That error appears when the list you are working on is empty, but i doublechecked and there is every file, but i also tryed with row[i][j].append(float(item) for item in line.split()) (that is the solution for the most common mistakes) and it returns me a really long list of this:

我真的搞不清问题出在哪里,我猜名单上没有填满,是max(我在网上看到有500.000.000个空格),我也看不出有什么问题。这个错误出现在您正在处理的列表为空的时候,但是我双重检查过,并且有每个文件,但是我也使用了行[i][j].append(float(item)(对象),用于行。split()(这是最常见错误的解决方案),它返回给我一个非常长的列表:

< generator object < genexpr> at 0x000000000XXXXXXX>

< generator object < genexpr> at 0x000000000xxxxxxxxx >

(Where the X are different numbers) ending then with the same error.

(X是不同的数字)以同样的错误结束。

P.s. If you need the data i'm working on just ask and i'll upload it.

如果你需要我正在处理的数据,只要问我就可以了,我会上传的。

1 个解决方案

#1


2  

It seems like your problem is pretty straightforward. You probably have a list named row and you're trying to assign values to non-existent indices

看来你的问题很简单。您可能有一个名为row的列表,您正在尝试为不存在的索引分配值

>>> row = []
>>> row[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

If however you appended a new list into the row list like I think you want to do

如果你像我想的那样把一个新的列表添加到行列表中

>>> row = []
>>> row.append([1, 2, 3, 4])
>>> row
[[1, 2, 3, 4]]
>>> row[0][0]
1

This would work fine. If you're trying to triple nest things

这工作很好。如果你想把窝数增加三倍

>>> row = []
>>> row.append([[1,2,3,4]])
>>> row
[[[1, 2, 3, 4]]]
>>> row[0].append([-1, -2, -3, -4])
>>> row
[[[1, 2, 3, 4], [-1, -2, -3, -4]]]

You can use the same syntax

您可以使用相同的语法

#1


2  

It seems like your problem is pretty straightforward. You probably have a list named row and you're trying to assign values to non-existent indices

看来你的问题很简单。您可能有一个名为row的列表,您正在尝试为不存在的索引分配值

>>> row = []
>>> row[0] = 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

If however you appended a new list into the row list like I think you want to do

如果你像我想的那样把一个新的列表添加到行列表中

>>> row = []
>>> row.append([1, 2, 3, 4])
>>> row
[[1, 2, 3, 4]]
>>> row[0][0]
1

This would work fine. If you're trying to triple nest things

这工作很好。如果你想把窝数增加三倍

>>> row = []
>>> row.append([[1,2,3,4]])
>>> row
[[[1, 2, 3, 4]]]
>>> row[0].append([-1, -2, -3, -4])
>>> row
[[[1, 2, 3, 4], [-1, -2, -3, -4]]]

You can use the same syntax

您可以使用相同的语法