首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python连接子字典

python连接子字典
EN

Stack Overflow用户
提问于 2020-05-20 16:49:01
回答 1查看 56关注 0票数 0

我正在尝试在python中连接子字典,这样就可以组成一个有效的json,我所拥有的是:

代码语言:javascript
复制
{
'ctx/language': 'en',
'ctx/territory': 'DE',
'composer_name': 'openEHR2study',
'Allergies': {
    'adverse_reaction-allergy': [{
            'reaction_event_summary': {
                'clinical_impact': [{
                        '|code': 'at0035'
                    }
                ]
            }
        }, {
            'recorded': ['2020-05-14T00:00:00.000Z']
        }, {
            'reaction_event_summary': {
                'certainty': [{
                        '|code': 'at0024'
                    }
                ]
            }
        }, {
            'substance_agent': ['s']
        }, {
            'reaction_reported': ['true']
        }, {
            'comment': ['c']
        }
    ]
}

}

我想要的是"reaction_event_summary“上的连接,如下所示:

代码语言:javascript
复制
{
'ctx/language': 'en',
'ctx/territory': 'DE',
'composer_name': 'openEHR2study',
'Allergies': {
    'adverse_reaction-allergy': [{
            'reaction_event_summary': {
                'clinical_impact': [{
                        '|code': 'at0035'
                    }
                ]
                 'certainty': [{
                        '|code': 'at0024'
                    }
                ]
            }
        }, {
            'recorded': ['2020-05-14T00:00:00.000Z']
        }, {
            'substance_agent': ['s']
        }, {
            'reaction_reported': ['true']
        }, {
            'comment': ['c']
        }
    ]
}

我不知道我应该如何遍历json/list和dicts来完成这项工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-20 17:32:49

我已经做了一次粗略的尝试,请检查一下是否有效。我们正试图用另一个深度副本来迭代字典。

代码语言:javascript
复制
val = {
    'ctx/language': 'en',
    'ctx/territory': 'DE',
    'composer_name': 'openEHR2study',
    'Allergies': {
        'adverse_reaction-allergy': [
            {
                'reaction_event_summary': {
                    'clinical_impact': [{
                        '|code': 'at0035'
                    }
                    ]
                }
            }, {
                'recorded': ['2020-05-14T00:00:00.000Z']
            }, {
                'reaction_event_summary': {
                    'certainty': [{
                        '|code': 'at0024'
                    }
                    ]
                }
            }, {
                'substance_agent': ['s']
            }, {
                'reaction_reported': ['true']
            }, {
                'comment': ['c']
            }
        ]
    }
}

import copy

val1 = copy.deepcopy(val)
del val1['Allergies']['adverse_reaction-allergy']
val1['Allergies']['adverse_reaction-allergy'] = []
reaction_count = 0

for _d in val['Allergies']['adverse_reaction-allergy']:
    if _d.get('reaction_event_summary', False):
        if reaction_count < 1:
            reaction_count += 1
            val1['Allergies']['adverse_reaction-allergy'].append(
                {'reaction_event_summary': _d.get('reaction_event_summary')})
        else:
            print(_d.get('reaction_event_summary'))
            _temp = val1['Allergies']['adverse_reaction-allergy'][0]['reaction_event_summary']
            _temp['certainty'] = _d.get('reaction_event_summary',{}).get('certainty',{})
            val1['Allergies']['adverse_reaction-allergy'][0]['reaction_event_summary'] = _temp
    else:
        val1['Allergies']['adverse_reaction-allergy'].append(_d)
import json
print(json.dumps(val1, indent=2))

输出示例

代码语言:javascript
复制
{
  "ctx/language": "en",
  "ctx/territory": "DE",
  "composer_name": "openEHR2study",
  "Allergies": {
    "adverse_reaction-allergy": [
      {
        "reaction_event_summary": {
          "clinical_impact": [
            {
              "|code": "at0035"
            }
          ],
          "certainty": [
            {
              "|code": "at0024"
            }
          ]
        }
      },
      {
        "recorded": [
          "2020-05-14T00:00:00.000Z"
        ]
      },
      {
        "substance_agent": [
          "s"
        ]
      },
      {
        "reaction_reported": [
          "true"
        ]
      },
      {
        "comment": [
          "c"
        ]
      }
    ]
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61908886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档