Python—如何将现有字典添加到一组新键

Python—如何将现有字典添加到一组新键,python,python-3.x,dictionary,Python,Python 3.x,Dictionary,我的函数为我提供以下格式的词典- { "key1": { "deleted": [], "inserted": [] }, "key2": { "deleted": [ { "end": 5, "line": "", "start": 5 } 但是,我希望得到如下输出- { "section1": {

我的函数为我提供以下格式的词典-

{
  "key1": {
    "deleted": [],
    "inserted": []
  },
  "key2": {
    "deleted": [
      {
        "end": 5,
        "line": "",
        "start": 5
      }
但是,我希望得到如下输出-

 {  "section1":
                    {
                        "name":"key1",
                        "deleted":[],
                        "inserted":[],
                    }
          },
    {  "section2":
                    {
                        "name":"key2",
                        "deleted":[],
                        "inserted":[],
                    }
          },

{  "section3":
                    {
                        "name":"key3",
                        "deleted":[],
                        "inserted":[],
                    }
          },
我所写的函数是-

diff_data = {}

    with open('2018temp.json', 'w') as f1t, open('2019temp.json', 'w') as f2t:
        json.dump(f1_dict, f1t)
        json.dump(f2_dict, f2t)

    for section in settings['includes']:
        f1_text = f1_dict.get(section, [])
        f2_text = f2_dict.get(section, [])
        diffile = []
        diff = difflib.Differ()
        with open('diffile.txt', 'a') as f:
            for line in diff.compare(f1_text, f2_text):
                f.write(line + ('\n' if not line.endswith('\n') else ''))
                if line.startswith(("-", "+", "?")):
                    diffile.append(line)

        data = {'deleted': [], 'inserted': []}

        for i, line in enumerate(diffile[:-1]):
            if line.startswith('-'):
                if diffile[i+1].startswith('?'): # Deletion and modification
                    updated_line = diffile[i+2]
                    update_start = re.search('[-+^]', diffile[i+1]).start()
                    update_end = re.search('[-+^][^-+^]*$', diffile[i+1]).start()
                    data['deleted'].append({'line': diffile[i+2][2:], 'start': update_start, 'end': update_end})
                elif diffile[i+1].startswith('+') and i+2 < len(diffile) and diffile[i+2].startswith('?'):
                    pass # Addition and modification, do nothing
                else:
                    data['deleted'].append(line[2:]) # Pure deletion
            elif line.startswith('+'):
                if diffile[i+1].startswith('?'): # Addition and modification
                    update_start = re.search('[-+^]', diffile[i+1]).start()
                    update_end = re.search('[-+^][^-+^]*$', diffile[i+1]).start()
                    data['inserted'].append({'line': line[2:], 'start': update_start, 'end': update_end})
                else:
                    data['inserted'].append(line[2:]) # Pure addition

        diff_data[section] = data

任何线索,我应该如何处理这个问题。这似乎是一个简单的问题,但我无法获得正确的格式作为所需的输出。如何编写一个循环,将现有字典作为值插入到新的一组关键字中-“section1,section2……”

我将复制dict,将原始关键字作为值引用
名称,然后创建一个新关键字引用新dict

diff_data = {}
i = 1

for key, d in x.items():
    new_dict = d.copy()
    new_dict['name'] = key
    new_key = "section_{}".format(str(i))
    diff_data[new_key] = new_dict
    i += 1

您想要的输出令人困惑。它不是一个带有键
第1节
第2节
等的单一dict;它似乎是一组独立的字典。@JohnGordon是的。。这就是问题所在,我应该有这种格式的输出,我有一个巨大的字典,因此,需要一种方法来编写循环,这样我就可以有这样的输出。这很令人困惑,因为它不符合你所说的你想要的。您想要一个有多个键的单张纸,还是想要多张单张纸,每个单张纸有一个键?你在自相矛盾。
diff_data = {}
i = 1

for key, d in x.items():
    new_dict = d.copy()
    new_dict['name'] = key
    new_key = "section_{}".format(str(i))
    diff_data[new_key] = new_dict
    i += 1