【发布时间】:2011-03-15 01:26:57
【问题描述】:
我正在寻找一种在不覆盖 levelA 的情况下使用 dict update 的内容更新 dict dictionary1 的方法
dictionary1={'level1':{'level2':{'levelA':0,'levelB':1}}}
update={'level1':{'level2':{'levelB':10}}}
dictionary1.update(update)
print dictionary1
{'level1': {'level2': {'levelB': 10}}}
我知道 update 会删除 level2 中的值,因为它正在更新最低的键 level1。
鉴于 dictionary1 和 update 可以有任意长度,我该如何解决?
【问题讨论】:
-
嵌套总是三层深还是可以嵌套任意深度?
-
它可以有任何深度/长度。
-
如果我错了,请纠正我,但这里的理想解决方案似乎需要实现复合设计模式。
标签: python