我希望能够在Python中向字典键添加多个值。该值是一个包含一些参数变化的字典。
我已经编写了以下代码,但似乎会覆盖之前的值。有没有更有效的方法来实现呢?
我已经编写了以下代码,但似乎会覆盖之前的值。有没有更有效的方法来实现呢?
from collections import defaultdict
node = 'node 1'
d = defaultdict(dict)
d[node] = {'interface':{'Eth1/48':'10.10.10.1'}} --> Here Eth1/48 is dynamically populated.
d[node] = {'interface':{'Eth1/47':'10.10.11.1'}} --> here Eth1/47 is again dynamically populated.
>>> d
defaultdict(<class 'dict'>, {'node 1': {'interface': {'Eth1/47': '10.10.11.1'}}})
我希望输出的结果类似于这样:
{'node 1':{'interface': {'Eth1/48':'10.10.10.1'}
{'Eth1/47':'10.10.11.1'}
{'Eth1/48':['10.0.0.1', '10.10.10.1']}
。 - erik258