简体   繁体   English

将单个字典的不同键合并到 Python 中的一个键

[英]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'}}

Use a ChainMap .使用ChainMap

>>> 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.

相关问题 Python,如何将三个字典合并为一个过滤键? - Python, how to merge three dictionary in one filtering for key? 如何将两个不同的 python 字典合并为一个字典? - How can i merge two different python dictionary into single one? 在 Python 中使用单个键合并两个不同长度的字典列表 - Merge two lists of dicts of different lengths using a single key in Python 如何将字典与一个键合并到 python 中的另一个键字典? - How to merge dictionary with a key to another key dictionary in python? 合并两个键值并将其分配给单个字典中的新键 - Merge two key values and assign it to a new key in a single dictionary python:我想通过相同的键合并不同的字典并将字典名称添加到键 - python: i want to merge different dictionaries by same key and add dictionary name to key 用Python中另一本词典的键值替换一个词典中的键 - Replace a key in one dictionary with the value of the key from another dictionary in Python 解析 Python 中的单个 qouted 字典键和值 - Parse single qouted dictionary key and value in Python 删除 python 字典中单个键的多个值 - Remove multiples values of a single key in python dictionary 具有多个值的 Python 字典单键可能吗? - Python dictionary single key with multiple values possible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM