使用点表示法扩展字典

时间:2018-12-31 02:15:35

标签: python recursion

我有以下json文档:

{
    "id": "5c26321bd8f4113d43b91141", 
    "idMemberCreator": "5b203bc7e47d817a8138bc37", 
    "data": {
        "list": {
            "name": "Sorji for QA", 
            "id": "5b0a2543b89acdbdb85f7b42"
        }, 
        "board": {
            "shortLink": "iyCzZ5jx", 
            "name": "FlicksIO", 
            "id": "5b0a251f68a9e74b8ec3b3ac"
        }, 
        "card": {
            "shortLink": "vOt2vO7v", 
            "idShort": 92, 
            "name": "New column in main for Storefront provider correlation.", 
            "id": "5b9c0023533f7c26424ea4ed", 
            "closed": true
        }, 
        "old": {
            "closed": false
        }
    }, 
    "type": "updateCard", 
    "date": "2018-12-28T14:24:27.455Z", 
    "limits": {}, 
    "memberCreator": {
        "id": "5b203bc7e47d817a8138bc37", 
        "avatarHash": "73bfa48c76c3c92615fe89ff79a6c5ae", 
        "avatarUrl": "https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae", 
        "fullName": "Marie Bond", 
        "idMemberReferrer": null, 
        "initials": "MB", 
        "username": "mb"
    }
}

我想将此扩展为带点表示法的单个级别。也就是说,它看起来应该像:

{
    "id": "5c26321bd8f4113d43b91141", 
    "idMemberCreator": "5b203bc7e47d817a8138bc37", 
    "data.list.name": "Sorji for QA", 
    "data.list.id": "5b0a2543b89acdbdb85f7b42"
    "data.board.shortLink": "iyCzZ5jx", 
    "data.board.name": "FlicksIO", 
    "data.board.id": "5b0a251f68a9e74b8ec3b3ac"
    "data.card.shortLink": "vOt2vO7v", 
    "data.card.idShort": 92, 
    "data.card.name": "New column in main for Storefront provider correlation.", 
    "data.card.id": "5b9c0023533f7c26424ea4ed", 
    "data.card.closed": true
    "data.old.closed": false
    "type": "updateCard", 
    "date": "2018-12-28T14:24:27.455Z", 
    "limits": {}, 
    "memberCreator.id": "5b203bc7e47d817a8138bc37", 
    "memberCreator.avatarHash": "73bfa48c76c3c92615fe89ff79a6c5ae", 
    "memberCreator.avatarUrl": "https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae", 
    "memberCreator.fullName": "Marie Bond", 
    "memberCreator.idMemberReferrer": null, 
    "memberCreator.initials": "MB", 
    "memberCreator.username": "mb"
}

是否可以使用生成器对象执行此操作?我今天在递归方面做了很多工作,并且一直在尝试从while循环转移到使用生成器对象和yield等。

1 个答案:

答案 0 :(得分:4)

您可以在递归函数的签名中保留一个参数来存储路径:

// add an Expanded widget to make sure long text wraps correctly.
Widget _wNameAndMessage = Expanded(
  child: Column(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      Text(name, style: Theme.of(context).textTheme.subhead),
      const SizedBox(height: 5.0),
      Text(message),
    ],
  ),
);

// ????
Widget _wNameAndMessage2 = Column(
  expanded: true,
  children: <Widget>[
    ...
  ],
);


Widget _wChatMessage = Container(
  margin: const EdgeInsets.symmetric(vertical: 10),
  child: Row(
    crossAxisAlignment: CrossAxisAlignment.start,
    children: <Widget>[
      _wAvator,
      const SizedBox(width: 16),
      _wNameAndMessage,
    ],
  ),
);

// ????
Widget _wChatMessage2 = Row(
  margin: const EdgeInsets.symmetric(vertical: 10),
  children: <Widget>[
  ...
  ],
);

data = {'id': '5c26321bd8f4113d43b91141', 'idMemberCreator': '5b203bc7e47d817a8138bc37', 'data': {'list': {'name': 'Sorji for QA', 'id': '5b0a2543b89acdbdb85f7b42'}, 'board': {'shortLink': 'iyCzZ5jx', 'name': 'FlicksIO', 'id': '5b0a251f68a9e74b8ec3b3ac'}, 'card': {'shortLink': 'vOt2vO7v', 'idShort': 92, 'name': 'New column in main for Storefront provider correlation.', 'id': '5b9c0023533f7c26424ea4ed', 'closed': True}, 'old': {'closed': False}}, 'type': 'updateCard', 'date': '2018-12-28T14:24:27.455Z', 'limits': {}, 'memberCreator': {'id': '5b203bc7e47d817a8138bc37', 'avatarHash': '73bfa48c76c3c92615fe89ff79a6c5ae', 'avatarUrl': 'https://trello-avatars.s3.amazonaws.com/73bfa48f79a6c5ae', 'fullName': 'Marie Bond', 'idMemberReferrer': None, 'initials': 'MB', 'username': 'mb'}}
def dot_paths(d, _paths = []):
  for a, b in d.items():
    if not b or not isinstance(b, dict):
      yield ['.'.join(_paths+[a]), b]
    else:
      yield from dot_paths(b, _paths+[a])

输出:

import json
print(json.dumps(dict(dot_paths(data)), indent=4))