如何将字典中的值与键列表匹配

时间:2019-04-12 07:39:20

标签: python list dictionary

我有一个名为的列表:

[
  'id','edge_media_to_caption','shortcode','edge_media_to_comment',
  'taken_at_timestamp','display_url','edge_liked_by','owner'
]

另一个带有嵌套字典的列表称为帖子,如下所示:

"posts": [
                    {
                        "node": {
                            "comments_disabled": "false",
                            "__typename": "GraphImage",
                            "id": "2018763372224677501",
                            "edge_media_to_caption": {
                                "edges": [
                                    {
                                        "node": {
                                            "text": "Advertisement | Soon it\u2019s festival season and I seriously can\u2019t wait to join Roskilde Festival once again! Good friends and a solid bag like this beautiful bumbag from @markberg_access is all you need (it has more space than you might think) \ud83c\udf7b\ud83d\ude4c\ud83c\udffd #markberg #we\u2764\ufe0felinor"
                                        }
                                    }
                                ]
                            },
                            "shortcode": "BwEFpdXBYZ9",
                            "edge_media_to_comment": {
                                "count": 2
                            },
                            "taken_at_timestamp": 1554875369,
                            "dimensions": {
                                "height": 1350,
                                "width": 1080
                            },
                            "display_url": "https://scontent-arn2-2.cdninstagram.com/vp/cefd572491a0c6c9f0822987f2107b88/5D41FE6F/t51.2885-15/e35/54731678_835552086798320_4599970429294248448_n.jpg?_nc_ht=scontent-arn2-2.cdninstagram.com",
                            "edge_liked_by": {
                                "count": 286
                            },
                            "edge_media_preview_like": {
                                "count": 286
                            },
                            "owner": {
                                "id": "1638100776"
                            }
                    }

如何遍历posts并与keys匹配? (帖子更长,但出于说明目的,我仅添加了一个节点。)

提前谢谢!

1 个答案:

答案 0 :(得分:1)

res = []
for post in posts:
    a = {}
    for key in keys:
        if key in post['node'].keys():
            a[key] = post['node'][key]
    res.append(a)

结果:

res = [
    {
        "id": "2018763372224677501",
        "edge_media_to_caption": {
            "edges": [
                {
                    "node": {
                        "text": "Advertisement | Soon it\u2019s festival season and I seriously can\u2019t wait to join Roskilde Festival once again! Good friends and a solid bag like this beautiful bumbag from @markberg_access is all you need (it has more space than you might think) \ud83c\udf7b\ud83d\ude4c\ud83c\udffd #markberg #we\u2764\ufe0felinor"
                    }
                }
            ]
        },
        "shortcode": "BwEFpdXBYZ9",
        "edge_media_to_comment": {
            "count": 2
        },
        "taken_at_timestamp": 1554875369,
        "display_url": "https://scontent-arn2-2.cdninstagram.com/vp/cefd572491a0c6c9f0822987f2107b88/5D41FE6F/t51.2885-15/e35/54731678_835552086798320_4599970429294248448_n.jpg?_nc_ht=scontent-arn2-2.cdninstagram.com",
        "edge_liked_by": {
            "count": 286
        },
        "owner": {
            "id": "1638100776"
        }
    }
]

但是仅适用于"node"

的键