IndexError:列表分配索引超出范围-带数组的Python [英] IndexError: list assignment index out of range - Python with an array

查看:74
本文介绍了IndexError:列表分配索引超出范围-带数组的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用python,但我仍然是该语言的许多新手.这段代码应该打印一系列的行(例如:[47.815、47.54、48.065、57.45]),这些行是我从几个文本文件(称为2t1,...,2t19,3t1,...,3t19)中输入的以及结束后的文件名.每个文件有80个必须保存的元素(总共〜12000个元素,每个文件〜80行,每行4个元素)

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)

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))

它能够执行我要求的操作,但是直到2t5才显示此消息:

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

回溯(最近通话最近一次):

Traceback (most recent call last):

文件<< tmp 1>",第8行,<模块>

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

row [i] [j] = [line.split()中项目的float(item)]

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

IndexError:列表分配索引超出范围

IndexError: list assignment index out of range

我真的无法弄清楚问题出在哪里,我想这个列表没有达到最大值(我在网上阅读说它有500.000.000个空格),而且我看不出有什么问题.当您正在处理的列表为空时,会出现该错误,但是我仔细检查了所有文件,但我也尝试过 row[i][j].append(float(item) for item in line.split()) (这是最常见错误的解决方案),它向我返回了很长的清单:

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:

<生成器对象< genexpr>在0x000000000XXXXXXX>

< generator object < genexpr> at 0x000000000XXXXXXX>

(其中X是不同的数字)以相同的错误结束,然后结束.

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

P.s.如果您需要我正在处理的数据,请询问并上传.

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

推荐答案

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

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[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]]]

您可以使用相同的语法

这篇关于IndexError:列表分配索引超出范围-带数组的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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