I have a nested dictionary and I want to replace a particular key. when I try to replace the key using replace, I get an error.
a = {'Stefan': [('__label__A', 0.90), ('__label__O', 0.06), ('__label__I', 0.01)],
'William': [('__label__A', 0.73), ('__label__B', 0.12), ('__label__U', 0.06)],
'James': [('__label__A', 0.63), ('__label__O', 0.35), ('__label__U', 0.01)]
}
_names = list(a.keys())
# for i in range(len(3)
tmp = a.get(_names[0])[0][0]
if tmp == '__label__W':
tmp = 'White'
elif tmp == '__label__A':
tmp = 'Asian'
elif tmp == '__label__B':
tmp = 'Black'
elif tmp == '__label__I':
tmp = 'Alaskan'
elif tmp == '__label__O':
tmp = 'other'
elif tmp == '__label__M':
tmp = 'Two races'
else:
tmp = 'Undesginated'
a.replace(a.get(_names[0])[0][0],tmp)
#(a.keys()).replace('__label__A','Asian')
#(a.get(_names[0])[0][0]).replace(tmp)
#a.replace('__label__A','Asian')
print(a)
I get error:
AttributeError: 'dict' object has no attribute 'replace'
The labels should be replaced as such
{'__label__W':'White', '__label__A': 'Asian',
'__label__B': 'Black','__label__I': 'Alaskan',
'__label__O': 'other','__label__M': 'Two races',
'__label__U': 'Undesginated'}
How to use replace command in this case or is there any better way ?