简体   繁体   English

IndexError:在python中列出索引超出范围,即使范围存在

[英]IndexError: list index out of range when in python, even when the range exists

I am sure that the range which I am trying to access exists, but still, the program shows error. 我确信我尝试访问的范围存在,但仍然,程序显示错误。 I am trying to make a program which will sum up the values in a column corresponding to the values in the other column. 我正在尝试创建一个程序,该程序将总结与另一列中的值对应的列中的值。

for example: 例如:

   28400    4
   28400    34 
   28400    9
   65478    2
   65478    5
   65478    3

what my program will do is, it will add up 4,34 and 9 and then it will add up 2,5 and 3 and then following will be the output- 我的程序将做的是,它将加起来4,34和9然后它将加起来2,5和3然后跟随将输出 -

   47
   47
   47
   10
   10
   10  

I am importing data from a CSV file. 我正在从CSV文件导入数据。 following is the code- 以下是代码 -

    import pandas as pd
    import numpy as np

    assessment = pd.read_csv('/home/user/Documents/MOOC dataset original/studentVle2.csv')


    assessment = assessment.values

    count=0
    stucount=28400
    sumc=[]
    i=0
    for stu in assessment[:,2:3]:
        if(stucount==stu):
            count = count + assessment[i,5]
            i=i+1
        else:
            sumc.append(count)
            count = 0
            count = count + assessment[i,5]
            i=i+1
        stucount=stu

    #print(sumc)

    stucount=28400
    i=0
    a=[]
    for stu in assessment[:,2:3]:
        if(stucount==stu):
            a.append(sumc[i])
            stucount = stu
        else:
            i=i+1
            a.append(sumc[i])
            stucount = stu

    print(a)

Error: 错误:

        File "/home/user/Documents/final project files/test.py", line 36, in <module>
        a.append(sumc[i])
        IndexError: list index out of range

and by the way, before adding some lines, like i=i+1,stucount=stu this error was not shown, but now it shows even though what happening is the same. 顺便说一句,在添加一些行之前,比如i = i + 1,stucount = stu这个错误没有显示,但是现在它显示了即使发生的事情是相同的。

The error is because you are not adding assessment value to sumc list for the last student after the loop ends. 该错误是因为您没有在循环结束后为最后一个学生的sumc列表添加评估值。 So, for n unique student id, the list length is only n-1 . 因此,对于n唯一的学生ID,列表长度仅为n-1 After for loop, add sumc.append(count) . for循环之后,添加sumc.append(count) See below. 见下文。

assessment = assessment.values

count=0
stucount=28400
sumc=[]
i=0
for stu in assessment[:,2:3]:
    if(stucount==stu):
        count = count + assessment[i,5]
        i=i+1
    else:
        sumc.append(count)
        count = 0
        count = count + assessment[i,5]
        i=i+1
    stucount=stu

sumc.append(count)
print(sumc)

stucount=28400
i=0
a=[]
for stu in assessment[:,2:3]:
    if(stucount==stu):
        a.append(sumc[i])
        stucount = stu
    else:
        a.append(sumc[i])
        stucount = stu
        i=i+1

print(a)

Place i=i+1 below stucount = stu and then try i=i+1置于stucount = stu下方,然后尝试

import pandas as pd
import numpy as np

assessment = pd.read_csv('/home/user/Documents/MOOC dataset original/studentVle2.csv')


assessment = assessment.values

count=0
stucount=28400
sumc=[]
i=0
for stu in assessment[:,2:3]:
    if(stucount==stu):
        count = count + assessment[i,5]
        i=i+1
    else:
        sumc.append(count)
        count = 0
        count = count + assessment[i,5]
        i=i+1
    stucount=stu

#print(sumc)

stucount=28400
i=0
a=[]
for stu in assessment[:,2:3]:
    if(stucount==stu):
        a.append(sumc[i])
        stucount = stu
    else:
        a.append(sumc[i])
        stucount = stu
        i=i+1

print(a)

The output will be different and change accordingly....error will be removed 输出将不同并相应更改....将删除错误

Here, i'm just going by your initial problem statement of what you have and what you want to get. 在这里,我只是按照您最初的问题陈述来了解您拥有的和您想要获得的内容。

df = pd.DataFrame([[28400,4],
                   [28400,34],
                   [28400,9],
                   [65478,2],
                   [65478,5],
                   [65478,3]], columns=list('AB'))
sums = df.groupby('A').B.sum()
df.A.map(sums)

And you get 你得到了

0    47
1    47
2    47
3    10
4    10
5    10
Name: A, dtype: int64

Was this what you were looking for? 这是你在找什么?

I think you should add i=i+1 after the error line a.append(sumc[i]) . 我想你应该在错误行a.append(sumc[i])之后添加i=i+1 Because in your code, may out of range of list at last. 因为在你的代码中,最后可能会超出列表范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM