Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中深度合并json的dicts/list_Python_Json_Dictionary_Merge - Fatal编程技术网

如何在python中深度合并json的dicts/list

如何在python中深度合并json的dicts/list,python,json,dictionary,merge,Python,Json,Dictionary,Merge,我正在尝试深度合并列表以获得特定的json 我想要实现的是这种格式(元素的顺序是I相关的): } 我尝试将目录和列表进行深度合并,以实现这种特定的格式。我的底线是一些格言: {'körpergewicht':[{'any_event_en':[{'gewicht':[{'Magnite':'100','unit':'kg'}]}] {'körpergewicht':[{'any_event_en':[{'bekleidung':[{'code':'at0013'}]}] {'körpergewi

我正在尝试深度合并列表以获得特定的json

我想要实现的是这种格式(元素的顺序是I相关的):

} 我尝试将目录和列表进行深度合并,以实现这种特定的格式。我的底线是一些格言:

{'körpergewicht':[{'any_event_en':[{'gewicht':[{'Magnite':'100','unit':'kg'}]}]

{'körpergewicht':[{'any_event_en':[{'bekleidung':[{'code':'at0013'}]}]

{'körpergewicht':[{'any_event_en':[{'kommentar':['none']}]}

{'context':[{'status':['fatty']}]}

{'context':[{'fallzahl':['123']}]}

{'context':[{'report_id':['123']}]}

{'context':[{'izahl':['123']}]}

我试着做的是,我有一个名为tmp_dict的dict,当我循环时,我拿着一个基线dict。所谓的collect_dict就是我试图合并基线dict的dict。元素保存我当前的基线dict

if (index == (len(element)-1)): #when the baseline dict is traversed completely
   if tmp_dict:
      first_key_of_tmp_dict=list(tmp_dict.keys())[0]
      if not (first_key_of_tmp_dict in collect_dict):
         collect_dict.update(tmp_dict)
      else:
         merge(tmp_dict,collect_dict)
   else:
      collect_dict.update(tmp_dict)

我还写了一个合并方法:

    def merge(tmp_dict,collect_dict):
        first_common_key_of_dicts=list(tmp_dict.keys())[0]
        second_depth_key_of_tmp_dict=list(tmp_dict[first_common_key_of_dicts][0].keys())[0]
        second_depth_tmp_dict=tmp_dict[first_common_key_of_dicts][0]
        second_depth_key_of_coll_dict=collect_dict[first_common_key_of_dicts][0]
        if not second_depth_key_of_tmp_dict in second_depth_key_of_coll_dict:
            collect_dict.update(second_depth_tmp_dict)
        else:
            merge(second_depth_tmp_dict,second_depth_key_of_coll_dict)
我所提出的方向是正确的,但远远没有达到我想要的结果:

{"report": {
                "k\u00f6rpergewicht": [{
                        "any_event_en": [{
                                "kommentar": ["none"]
                            }
                        ],
                        "bekleidung": [{
                                "|code": "at0013"
                            }
                        ],
                        "gewicht": [{
                                "|magnitude": "100",
                                "|unit": "kg"
                            }
                        ]
                    }
                ],
                "context": [{
                        "fallzahl": ["234"]
                    }
                ],
                "report_id": ["234"],
                "status": ["s"],
                "izahl": ["234"]
            }
}
使用另一组输入:

{'atemfrequenz': {'context': [{'status': [{'|code': 'at0012'}]}]}},
{'atemfrequenz': {'context': [{'kategorie': ['Kategorie']}]}},
{'atemfrequenz': {'atemfrequenz': [{'messwert': [{'|magnitude': '123', '|unit': 'min'}]}]}}

我希望实现以下输出:

"atemfrequenz": {
    "context": [
      {
        "status": [
          {
            "|code": "at0012"
          }
        ],
        "kategorie": [
          "Kategorie"
        ]
      }
    ],
    "atemfrequenz": [
      {
        "messwert": [
          {
            "|magnitude": 123,
            "|unit": "/min"
          }
        ]
      }
    ]
  }

这段代码应该得到正确的答案。为了防止错误,我删除了特殊字符(ö)

dd = [
{'korpergewicht': [{'any_event_en': [{'gewicht': [{'|magnitude': '100', '|unit': 'kg'}]}]}] },
{'korpergewicht': [{'any_event_en': [{'bekleidung': [{'|code': 'at0013'}]}]}]},
{'korpergewicht': [{'any_event_en': [{'kommentar': ['none']}]}]},
{'context': [{'status': ['fatty']}]},
{'context': [{'fallzahl': ['123']}]},
{'context': [{'report_id': ['123']}]},
{'context': [{'izahl': ['123']}]}
]


def merge(d):
   if (type(d) != type([])): return d
   if (type(list(d[0].values())[0])) == type(""): return d
   keys = list(set(list(k.keys())[0] for k in d))
   lst = [{k:[]} for k in keys]
   for e in lst:
       for k in d:
           if (list(e.keys())[0] == list(k.keys())[0]):
               e[list(e.keys())[0]] += k[list(k.keys())[0]]
   for e in lst:
       if (type(e[list(e.keys())[0]][0]) == type({})):
           e[list(e.keys())[0]] = merge(e[list(e.keys())[0]]) 
       for i in lst[1:]: lst[0].update(i)
   lst2 = []  # return list of single dictionary
   lst2.append(lst[0])
   return lst2

dx = merge(dd)
dx = {'report': dx[0]} # no list at lowest level
print(dx)
输出(格式化)

关于提供的第二个数据集,需要对数据进行结构化,以匹配前一个数据集

此数据集工作正常:

dd = [
{'atemfrequenz': [{'context': [{'status': [{'|code': 'at0012'}]}]}]}, 
{'atemfrequenz': [{'context': [{'kategorie': ['Kategorie']}]}]}, 
{'atemfrequenz': [{'atemfrequenz': [{'messwert': [{'|magnitude': '123', '|unit': 'min'}]}]}]} 
] 
输出(格式化)


在您的回答中,这些行是dict,但它们应该比每一行都多一个dict,因为dict列表如下:“körpergewicht”:[{“any_event_en”:[{“gewich”:[{“magnity”:185.44,“| unit”:“kg”),“kommentar”:[“kommentar 94”,“bekleidung”:[{“code”:“at0011”]在我使用这些输入之前,您的代码对我来说运行良好:'[{'atemfrequenz':{'context':[{'status':[{'atemfrequenz':'at0012'}]}},{'atemfrequenz':{'context':[{'kategorie':['kategorie']}},{'atemfrequenz':{'atemfrequenz':[{'messwert':[{{'124;震级':'123','单位':'min'}}}}}},现在我在合并后得到这个[{'atemfrequenz':'如果您对数据结构做了一个小的更改,它会正确处理。我不知道您上次的更改在哪里不能正常工作,我更新了我的问题,以获得更好的概述。
{'report': {
     'korpergewicht': [{
          'any_event_en': [{
                'kommentar': ['none'], 
                'bekleidung': [{'|code': 'at0013'}], 
                'gewicht': [{'|magnitude': '100', '|unit': 'kg'}]}]}], 
      'context': [{
          'report_id': ['123'], 
          'izahl': ['123'], 
          'fallzahl': ['123'], 
          'status': ['fatty']}]}}
dd = [
{'atemfrequenz': [{'context': [{'status': [{'|code': 'at0012'}]}]}]}, 
{'atemfrequenz': [{'context': [{'kategorie': ['Kategorie']}]}]}, 
{'atemfrequenz': [{'atemfrequenz': [{'messwert': [{'|magnitude': '123', '|unit': 'min'}]}]}]} 
] 
{'report': {
    'atemfrequenz': [{
         'atemfrequenz': [{
              'messwert': [{'|magnitude': '123', '|unit': 'min'}]}], 
    'context': [{
         'kategorie': ['Kategorie'], 
         'status': [{'|code': 'at0012'}]}]}]}}