简体   繁体   中英

How to match values from a dictionary to a list of keys

I have a list called keys :

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

And another list with nested dictionaries called posts , looking something like this:

"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"
                            }
                    }

How do I loop through posts and match with the keys ? (posts is much longer, but I only added one node for the illustration purposes).

Thank you in advance!

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

And result:

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"
        }
    }
]

But in works only for keys of "node"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM