我得到这个错误索引器:列表索引超出范围

2024-09-30 20:28:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想加载numpy个文件以输入CNN,但我得到了这个IndexError。我不知道为什么

任何帮助都将不胜感激

 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

Tags: 文件numpyleniflinecnnsplitprint
1条回答
网友
1楼 · 发布于 2024-09-30 20:28:18

由于您没有发布所有相关的代码,因此很难给您提供一个有保证的解决方案,但我猜想您在执行第一个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

相关问题 更多 >