合并两个以字典为键值的字典
我的问题与此相似,但是答案却没有答案。 t产生正确的结果(对我来说?)。
以这些词典为例:
a = {'a': {'a': 1}}
b = {'a': {'b': 2}}
我想产生:
c = {'a': {'a': 1, 'b': 2}}
使用引用问题的答案,所有这些都会产生:
c = a.copy()
c.update(b)
>>
c == {'a': {'b': 2}
考虑A和B可能比这更复杂,例如:
a = {'a': {'aa': {'aaa': 1}, 'bb': {'bbb': 2}}}
b = {'a': {'bb': {'aaa': 1}, 'bb': {'bbb': 2}}}
Merge two dictionaries of dictionaries
My question is similar to this one, but the answers don't produce the right result (for me?).
Take these dictionaries:
a = {'a': {'a': 1}}
b = {'a': {'b': 2}}
I want to produce:
c = {'a': {'a': 1, 'b': 2}}
Using the answers from the quoted question, these all produce:
c = a.copy()
c.update(b)
>>
c == {'a': {'b': 2}
Consider that a and b might be more complex than this, for example:
a = {'a': {'aa': {'aaa': 1}, 'bb': {'bbb': 2}}}
b = {'a': {'bb': {'aaa': 1}, 'bb': {'bbb': 2}}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
In this case you can use
Element in dictionary is also dictionary, so you can treat that element as dictionary.
至于更复杂的例子,我不知道应该是什么结果。但是通常,您可以访问元素中的元素作为嵌套循环中的字典。
In this case you can use
Element in dictionary is also dictionary, so you can treat that element as dictionary.
As for more complex example I don't know what result should be. But in general, you can access elements in element as dictionary in nested for loops.