如何在以字典为值的python中编辑字典值? [英] how to edit a dictionary value in python that have a dictionary as a value?
本文介绍了如何在以字典为值的python中编辑字典值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试用python制作一个字典,该字典具有一个键和一个像这样的值的字典
I'm trying to make a dictionary in python that has a key and a dictionary as a value like that
a = ('emp1','emp2')
b = ({'dep':'', 'sal':''})
comp = dict.fromkeys(a,b)
print(comp)
comp['emp1']['dep'] = 'IT'
comp['emp1']['sal'] = '300$'
print(comp)
输出就是这样
{'emp1': {'dep': '', 'sal': ''}, 'emp2': {'dep': '', 'sal': ''}}
{'emp1': {'dep': 'IT', 'sal': '300$'}, 'emp2': {'dep': 'IT', 'sal': '300$'}}
如果我仅尝试为 "emp1"
更改 值 ,为什么所有值都在更改 谁能帮忙???
why all the values are changing if I'm trying to chang the value only for "emp1"
can any one help ???
推荐答案
查看全文