Search code examples
pythondictionarykeyerror

KeyError 43L in python


I want to assign a list for dictionary inside another dictionary but i get a key error you can find the file here https://www.dropbox.com/s/9n35b41rzuxjkk8/plan1.txt?dl=0

Code, Title, Lec, Lab, Cr, Prereq, Year, Semester

ARAB110 ,  Arabic Language Skills I ,  3 ,  0 , 3,  - ,  1 ,  1 
CHEMY101 ,  General Chemistry I ,  3 ,  3 , 4,  - ,  2 ,  2 
ENGL154 ,  Language Development I ,  3 ,  0 , 3,  - ,  1 ,  1 
ENGL155 ,  Language Development II ,  3 ,  0 , 3,  ENGL154 ,  1 ,  2 
ENGL219 ,  Technical Report Writing ,  3 ,  0 , 3,  ENGL155 ,  2 ,  1 

i want to create a dict for every year and inside every year a dictonary of every semester contains a list of all codes in this semester

curri = read_csv('senior_data/plan1.txt')
curri = curri.sort([' Year',' Semester '],ascending=[1,1]) 

cur = {}
cur['1']={}
cur['2']={}
cur['3']={}
cur['4']={}

cur['1']['1'] = {}
cur['1']['2'] = {}
cur['2']['1'] = {}
cur['2']['2'] = {}
cur['3']['1'] = {}
cur['3']['2'] = {}
cur['3']['3'] = {}
cur['4']['1'] = {}
cur['4']['2'] = {}


i=0
c_list = []
for xxx in cur:
    for y in cur[xxx]:
        print (xxx+" "+y)
        for row in curri.iterrows():
            if(str(curri[' Year'][i]) == xxx and str(curri[' Semester '][i] == y)):   
                print (xxx+":"+y+ curri['Code'][i])
                c_list.append(curri['Code'][i])
            i=i+1
            cur[xxx][y] = c_list
            c_list = []

when I remove the third loop it's work fine!

this is the output:

1 1
1:1 ARAB110 
1:1 ENGL154 
1:1 ENGL155 
1:1 ISLM101 
1:1 ITCS101 
1:1 ITCS102 
1:1 MATHS101 
1:1 MATHS102 
1:1 PHYCS101 
1:1 PHYCS102 
1 2

the error is

KeyError: 42L

---> 24             if(str(curri[' Year'][i]) == xxx and str(curri[' Semester ']

Solution

  • Your increment of ishould happen in the most inner loop.

    Assuming that the rows are dictionaries, this could work:

    c_list = []
    for xxx in cur:
        for y in cur[xxx]:
            print (xxx+" "+y)
            for row in curri.iterrows():
                if(str(row[' Year']) == xxx and str(row[' Semester '] == y)):   
                    print (xxx+":"+y+ row['Code'])
                    c_list.append(row['Code'])
                cur[xxx][y] = c_list
                c_list = []
    

    If not try this:

    c_list = []
    for xxx in cur:
        for y in cur[xxx]:
            print (xxx+" "+y)
            i = 0
            for row in curri.iterrows():
                if(str(curri[' Year'][i]) == xxx and str(curri[' Semester '][i] == y)):   
                    print (xxx+":"+y+ curri['Code'][i])
                    c_list.append(curri['Code'][i])
                i=i+1
                cur[xxx][y] = c_list
                c_list = []
    

    In Python it is typically not necessary to increment an index by hand:

    c_list = []
    for xxx in cur:
        for y in cur[xxx]:
            print (xxx+" "+y)
            for i, row in enumerate(curri):
                if(str(curri[' Year'][i]) == xxx and str(curri[' Semester '][i] == y)):   
                    print (xxx+":"+y+ curri['Code'][i])
                    c_list.append(curri['Code'][i])
                cur[xxx][y] = c_list
                c_list = []