python 嵌套中的字典赋值

l=["x1","x2","x3"]
l2=["ff","ffs"]
dic1=dict.fromkeys(l,dict.fromkeys(l2,0))
构造的嵌套字典如下:
{'x1': {'ff': 0, 'ffs': 0},
'x2': {'ff': 0, 'ffs': 0},
'x3': {'ff': 0, 'ffs': 0}}
现在想对key=x1的item中的“ff”重新赋值为1
即期望结果是
{'x1': {'ff': 1, 'ffs': 0},
'x2': {'ff': 0, 'ffs': 0},
'x3': {'ff': 0, 'ffs': 0}}
使用语句
dic1["x1"]['ff']=1
但是输出结果是 print(dic1)
{'x1': {'ff': 1, 'ffs': 0},
'x2': {'ff': 1, 'ffs': 0},
'x3': {'ff': 1, 'ffs': 0}}

请问怎么修改

yourDict={'1000':{'1':['a','b','c','d'],'2':['e','b','c','a']},'2000':{'1':['c','d','c','d'],'2':['a','a','c','d']}} out=open('out.xls','w') for key in yourDict: out.write(key) for key2 in yourDict[key]: out.write('\t') out.write(key2+'\t') out.write('\t'.join(yourDict[key][key2] )) out.write('\n') 最后xls转存为csv即可
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-06-18
那是因为你的每一个key对应的dict都是同一个,可以用id看一下,是完全相同的。
至于修改么。。。循环赋值吧,保证每次可以新建一个
第2个回答  2018-04-29
dct1.update({"x1":{"ff":1,"ffs":0}})

笨方法,好用就拿去