python - Type error when trying to access the dict value -
i have dict
d1={'a':['in-gaap','inr',0,1],'b':['in-gaap','inr',0,2],'c':['in-ca','share',0,4],'n1':['','','','aaa']} d2={'d':['in-gaap','inr',0,'a+b'],'e':['in-gaap','inr',0,'y+t']} k in d2.iterkeys(): a=re.findall('\w+',d2[k][3]) x2=dict([(x,d1.get(x,0)[3])for x in a]) # here showing type:error int obj not subscriptable d1[k]=[d2[k][0],d2[k][1],d2[k][2],eval(d2[k][3],x2)]
'a' list dynamically created splits d[k][3] , stores in "a" d[k][3] contains in first iteration a=['a','b'] , in second iteration a=['y','t'] comparing list "a" keys dict "d1" keys if key takes value or assigns default value,upto working fine. when try create dict comparing list "a" dict "d1" ,by using code x2=dict([(x,d1.get(x,0)[3])for x in a])
it shows type:error int object not subsricptable. dono y d2[k][3] has value showing error.
d1.get(x,0)[3]
. problem. if x
isn't in d1
, d1.get(x,0)
returns 0
. , 0[3]
typeerror.
replace problem line these lines, , should work, assuming read intent correctly:
values = [d1[x][3] if x in d1 else 0 x in a] x2 = dict(zip(a, values))
this easier read too.
Comments
Post a Comment