[英]Merge different key of single Dictionary to one key in Python
I have a dictionary like:我有一本像这样的字典:
d = {
'S1': {
'S11': {'first': 'a', 'second': 'b'},
'S12': {'first': 'c', 'second': 'd'}
},
'S2': {
'S21': {'first': 'l', 'second': 'e'},
'S22': {'first': 'd', 'second': 't'}
},
'S3': {
'S31': {'first': 'z', 'second': 'p'},
'S32': {'first': 'x', 'second': 'g'}
}
}
I want to merge this dict like:我想像这样合并这个字典:
{
'S': {
'S11': {'first': 'a', 'second': 'b'},
'S12': {'first': 'c', 'second': 'd'},
'S21': {'first': 'l', 'second': 'e'},
'S22': {'first': 'd', 'second': 't'},
'S31': {'first': 'z', 'second': 'p'},
'S32': {'first': 'x', 'second': 'g'}
}
}
The problem is in actual I have s1, s2, s3.....s100 and my current method is not very clear.问题实际上是我有s1,s2,s3.....s100,我目前的方法不是很清楚。 Could someone please suggest a better way to achieve this?有人可以提出一个更好的方法来实现这一目标吗?
Thanks谢谢
You can try the following:您可以尝试以下方法:
d["S"] = {}
for i in d.keys():
d["S"].update(d[i])
Which returns:返回:
{'S11': {'first': 'a', 'second': 'b'}, 'S12': {'first': 'c', 'second': 'd'}, 'S21': {'first': 'l', 'second': 'e'}, 'S22': {'first': 'd', 'second': 't'}, 'S31': {'first': 'z', 'second': 'p'}, 'S32': {'first': 'x', 'second': 'g'}}
>>> from collections import ChainMap
>>> c = {'S': ChainMap(*d.values())}
>>> c['S']['S32']
{'first': 'x', 'second': 'g'}
You could convert the ChainMap
back to a dict, if you like.如果您愿意,可以将ChainMap
转换回字典。
>>> c = {'S': dict(ChainMap(*d.values()))}
>>> c
{'S': {
'S31': {'first': 'z', 'second': 'p'},
'S32': {'first': 'x', 'second': 'g'},
'S21': {'first': 'l', 'second': 'e'},
'S22': {'first': 'd', 'second': 't'},
'S11': {'first': 'a', 'second': 'b'},
'S12': {'first': 'c', 'second': 'd'}}
}
This can be done with a simple nested loop over the dict items.这可以通过 dict 项上的简单嵌套循环来完成。
new_d = {'S': {}}
for k, v in d.items():
for k1, v1 in v.items():
new_d['S'][k1] = v1
d['S']=new_d['S']
print(d['S'])
#Output
{'S': {'S11': {'first': 'a', 'second': 'b'}, 'S12': {'first': 'c', 'second': 'd'}, 'S21': {'first': 'l', 'second': 'e'}, 'S22': {'first': 'd', 'second': 't'}, 'S31': {'first': 'z', 'second': 'p'}, 'S32': {'first': 'x', 'second': 'g'}}}
Try this,尝试这个,
data = {}
for (k, v), j in d.items():
if data.get(k):
data[k].update(j)
else:
data[k] = j
from pprint import pprint as pp
d = {
"S1": {"S11": {"first": "a", "second": "b"}, "S12": {"first": "c",
"second": "d"}},
"S2": {"S21": {"first": "l", "second": "e"}, "S22": {"first": "d", "second": "t"}},
"S3": {"S31": {"first": "z", "second": "p"}, "S32": {"first": "x", "second": "g"}},
}
# Take out the values first
values = d.values()
# Use the values in the new dict
d_trans = {"S": values}
# Print the result
pp(d_trans)
You can simply do this in one line:-您可以简单地在一行中执行此操作:-
res = {'S': {k1:v1 for v in d.values() for k1, v1 in v.items()}}
print(res)
Output:- Output:-
{
'S': {
'S11': {'first': 'a', 'second': 'b'},
'S12': {'first': 'c', 'second': 'd'},
'S21': {'first': 'l', 'second': 'e'},
'S22': {'first': 'd', 'second': 't'},
'S31': {'first': 'z', 'second': 'p'},
'S32': {'first': 'x', 'second': 'g'}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.