Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我收到此错误-索引器:列表索引超出范围_Python_List_Numpy_Split_Append - Fatal编程技术网

Python 我收到此错误-索引器:列表索引超出范围

Python 我收到此错误-索引器:列表索引超出范围,python,list,numpy,split,append,Python,List,Numpy,Split,Append,我想加载numpy文件以输入CNN,但我得到了这个索引器。我不知道为什么preictalspectrograms[cont].append(line.split(“”)[2].rstrip())索引器:列表索引超出范围 任何帮助都将不胜感激 while(line.strip()!=""): print('linex: ', line) print('\n') if("SEIZURE" in line):

我想加载
numpy
文件以输入CNN,但我得到了这个
索引器
。我不知道为什么
preictalspectrograms[cont].append(line.split(“”)[2].rstrip())索引器:列表索引超出范围

任何帮助都将不胜感激

 while(line.strip()!=""):
        print('linex: ', line)
        print('\n')
        if("SEIZURE" in line):
            line=f.readline()
            if(len(line.split(' '))>=3):
                preictalSpectograms.append([])
                cont=cont+1
                preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
                indFilePathRead=indFilePathRead+1
        else:
            if(len(line.split(' '))>=3):
                preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
            indFilePathRead=indFilePathRead+1

由于您没有发布所有相关的代码,因此很难给您提供一个有保证的解决方案,但我的猜测是,您在执行第一个
append()
调用之前就在增加
cont
,因此您实际上从未追加到列表中的第一项

while(line.strip()!=""):
        print('linex: ', line)
        print('\n')
        if("SEIZURE" in line):
            line=f.readline()
            if(len(line.split(' '))>=3):
                preictalSpectograms.append([])
                preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
                indFilePathRead=indFilePathRead+1
                cont=cont+1

        else:
            if(len(line.split(' '))>=3):
                preictalSpectograms[cont].append(line.split(' ')[2].rstrip())
            indFilePathRead=indFilePathRead+1

我的猜测是cont中出现的任何数字都大于preictalspectrograms的大小请提供一个包含两个列表索引操作的行,
preictalspectrograms[cont]
line.split(“”)[2]
。你的工作是确定哪些是问题。前面的
if
行应该可以防止第二行出现问题(但为什么要拆分两次?)。在这种情况下,
cont
必须关闭。您是附加到
前庭频谱图本身,还是附加到其中的列表?您需要逐行、逐次地测试代码块。不要猜测发生了什么事。测试,打印,测试,再打印!顺便问一下,你所说的
numpy文件
是什么意思?它们是
csv
格式文本吗?