![](/img/trans.png)
[英]How to append multiple key and value into a nested dictionary using python?
[英]How to manipulate a nested dictionary and append a key to it in python?
我有一本字典,它是一個默認的字典。 我想通過遍歷一個列表將一個鍵附加到嵌套的 dict 中。 不是:new_dict 是一個默認的 dict,它的定義如下new_dict = defaultdict(lambda: defaultdict(list))
from collections import defaultdict
new_dict = {'A': {'10.xxx.77.1': [7], '10.xxx.77.2': [8]}, 'B': {'10.xxx.77.1': [5], '10.xxx.77.2': [6]}}
positions = ['left','right']
for position in positions:
for k, v in new_dict.items():
if isinstance(v, dict):
v[position].extend(v)
print(new_dict)
實際輸出:{'A': {'10.xxx.77.1': [5], '10.xxx.77.2': [6], 'left': ['10.xxx.77.1', '10.xxx .77.2', 'left'], 'right': ['10.xxx.77.1', '10.xxx.77.2', 'left', 'right']}, 'B': {'10.xxx. 77.1': [7], '10.xxx.77.2': [8], 'left': ['10.xxx.77.1', '10.xxx.77.2', 'left'], 'right': [ '10.xxx.77.1'、'10.xxx.77.2'、'左'、'右']}}
{'A': {'10.xxx.77.1': [5], '10.xxx.77.2': [6], 'left': ['10.xxx.77.1', '10.xxx.77.2' , 'left'], 'right': ['10.xxx.77.1', '10.xxx.77.2', 'left', 'right']}, 'B': {'10.xxx.77.1': [7], '10.xxx.77.2': [8], 'left': ['10.xxx.77.1', '10.xxx.77.2', 'left'], 'right': ['10. xxx.77.1', '10.xxx.77.2', '左', '右']}}
預期輸出
{'A': {'left': { '10.xxx.77.1', [5]},'right': {'10.xxx.77.2', [6]}},'B': {'left': {'10.xxx.77.1', [7]},'right': {'10.xxx.77.2', [8]}}}
如果你想元組作為值:
from collections import defaultdict
new_dict = {'A': {'10.xxx.77.1': [7], '10.xxx.77.2': [8]}, 'B': {'10.xxx.77.1': [5], '10.xxx.77.2': [6]}}
positions = ['left','right']
for k, v in new_dict.items():
if isinstance(v, dict):
new_dict[k] = {positions[0]:tuple(list(new_dict[k].items())[0]), positions[1]:tuple(list(new_dict[k].items())[1])}
print(new_dict)
輸出為值元組:
{'A': {'left': ('10.xxx.77.1', [7]), 'right': ('10.xxx.77.2', [8])}, 'B': {'left': ('10.xxx.77.1', [5]), 'right': ('10.xxx.77.2', [6])}}
如果你想字典作為值:
from collections import defaultdict
new_dict = {'A': {'10.xxx.77.1': [7], '10.xxx.77.2': [8]}, 'B': {'10.xxx.77.1': [5], '10.xxx.77.2': [6]}}
positions = ['left','right']
for k, v in new_dict.items():
if isinstance(v, dict):
new_dict[k] = {positions[0]:{tuple(list(new_dict[k].items())[0])[0]:tuple(list(new_dict[k].items())[0])[1]}, positions[1]:{tuple(list(new_dict[k].items())[1])[0]:tuple(list(new_dict[k].items())[1])[1]}}
print(new_dict)
輸出為值字典:
{'A': {'left': {'10.xxx.77.1': [7]}, 'right': {'10.xxx.77.2': [8]}}, 'B': {'left': {'10.xxx.77.1': [5]}, 'right': {'10.xxx.77.2': [6]}}}
這將返回預期的輸出,但這取決於new_dict[k].items()
返回的序列的順序,這並不總是您想要的。 為了確保正確性,您應該通過sorted
函數對其進行sorted
,因此左側表示左側,右側表示右側,但是從您的問題中不清楚這些假設是什么意思。
from collections import defaultdict
new_dict = defaultdict(lambda: defaultdict(list))
new_dict.update({'A': {'10.xxx.77.1': [7], '10.xxx.77.2': [8]}, 'B': {'10.xxx.77.1': [5], '10.xxx.77.2': [6]}})
positions = ['left','right']
for k, v in new_dict.items():
if isinstance(v, dict):
new_dict[k] = {pos: {key: val} for pos, (key, val) in zip(positions, new_dict[k].items())}
print(dict(new_dict))
結果:
{'A': {'left': {'10.xxx.77.1': [7]}, 'right': {'10.xxx.77.2': [8]}}, 'B': {'left ':{'10.xxx.77.1':[5]},'右':{'10.xxx.77.2':[6]}}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.