【发布时间】:2020-04-08 12:07:59
【问题描述】:
共有三种不同的嵌套字典:
dict1={'g1': {'s1': 'checked', 's2': '0', 'Both':'checked'}, 'g2': {'s1': '0', 's2':'checked', 'Both': 'checked'}, 'g3': {'s1': 'checked', 's2': 'checked', 'Both': 'checked'}}
dict2={'g1': {'s1': '11', 's2': '', 'Both': '13'}, 'g2': {'s1': '', 's2': '22', 'Both':'23'}, 'g3': {'s1':'31', 's2': '32', 'Both': '33'}}
dict3={"s1": {"PS":'value1'}, "s2": {"PS": 'value2'}, "Both": {"PS": 'value3'}}
所需的嵌套字典是dict4。 (注意:dict1 或 dict2 的格式,即{'g1': {'s1': "xxxx",'s2':'xxxy'}},其中xxxx 和xxxy 是来自以下给定条件的值。)
描述:键和值是通用的。并且上述三个字典所需的所需输出也必须是嵌套字典,条件为 if value=='checked' in dict1 然后 dict3 具有新键 'Msg' 和来自 dict2 的值必须添加相同的嵌套键到所需的嵌套字典dict4,其父键来自dict1。
我的代码:
dict4={} # declaring desired dictionary
for nkey,nvalue in dict1.items():
tempDict={} #temporary dictionary to store all checked from dict1
for key,value in nvalue.items():
if value=='checked':
tempDict[key]=dict3[key]
tempDict[key]['Msg']=dict2[nkey][key]
#in prescribed format assigning dict4
dict4[nkey]=tempDict
上述代码的错误输出:(错误的'Msg' 值)
dict4 = {'g1': {'s1': {'PS': 'value1', 'Msg': '31'}, 'Both': {'PS': 'value3', 'Msg': '33'}}, 'g2': {'s2': {'PS': 'value2', 'Msg': '32'}, 'Both': {'PS': 'value3', 'Msg': '33'}}, 'g3': {'s1': {'PS': 'value1', 'Msg': '31'}, 's2': {'PS': 'value2', 'Msg': '32'}, 'Both': {'PS': 'value3', 'Msg': '33'}}}
正确和期望的输出:(带有正确的'Msg')
dict4 = {'g1': {'s1': {'PS': 'value1', 'Msg': '11'}, 'Both': {'PS': 'value3', 'Msg': '13'}}, 'g2': {'s2': {'PS': 'value2', 'Msg': '22'}, 'Both': {'PS': 'value3', 'Msg': '23'}}, 'g3': {'s1': {'PS': 'value1', 'Msg': '31'}, 's2': {'PS': 'value2', 'Msg': '32'}, 'Both': {'PS': 'value3', 'Msg': '33'}}}
根据上面的代码,我试图将所有检查过的 dict1 存储到临时字典中,然后添加键 'Msg'(新键)和来自 dict2 的值,然后以所需格式分配 dict4 (来自dict1 的键和来自临时字典的值,即tempDict)。
问题:正如我所观察到的那样,它会在更改时从所需的 dict4 和 tempDict 值覆盖 'Msg',然后在 for 循环内部反映 dict4 值(因为我通过打印来调试代码所有阶段的值)。
请澄清并提出可能的解决方案,以及为什么当tempDict 上的值发生变化时,它反映到已经分配给dict4 中的前一个键值,即当tempDict 更改'Msg' 为@987654354 时@ 然后它也会更改 g1 和 g2 中的值。
我已经尝试用tempDict.clear() 方法代替tempDict={} 然后dict4 输出是这样的:
dict4={} # declaring desired dictionary
for nkey,nvalue in dict1.items():
tempDict={} #temporary dictionary to store all checked from dict1
for key,value in nvalue.items():
if value=='checked':
tempDict[key]=dict3[key]
tempDict[key]['Msg']=dict2[nkey][key]
#in prescribed format assigning dict4
dict4[nkey]=tempDict
tempDict.clear()
上面的输出:(错误)
dict4 = {'g1': {}, 'g2': {}, 'g3': {'s1': {'PS': 'value1', 'Msg': '31'}, 's2': {'PS': 'value2', 'Msg': '32'}, 'Both': {'PS': 'value3', 'Msg': '33'}}},
是否存在从价值问题或我写错逻辑的地方分配的内存?
正确且期望的输出:(带有正确的'Msg')
dict4 = {'g1': {'s1': {'PS': 'value1', 'Msg': '11'}, 'Both': {'PS': 'value3', 'Msg': '13'}}, 'g2': {'s2': {'PS': 'value2', 'Msg': '22'}, 'Both': {'PS': 'value3', 'Msg': '23'}}, 'g3': {'s1': {'PS': 'value1', 'Msg': '31'}, 's2': {'PS': 'value2', 'Msg': '32'}, 'Both': {'PS': 'value3', 'Msg': '33'}}}
代码 3:(相同的输出)
dict4={}
for nkey,nvalue in dict1.items():
for key,value in nvalue.items():
if value=='checked':
try:
dict4[nkey][key]=dict3[key]
except:
dict4[nkey]={}
dict4[nkey][key]=dict3[key]
dict4[nkey][key]['Msg']=dict2[nkey][key]
【问题讨论】:
标签: python dictionary for-loop nested