Как сопоставить значения из словаря со списком ключей - PullRequest
0 голосов
/ 12 апреля 2019

У меня есть список с именем keys :

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

И еще один список с вложенными словарями под названием posts , выглядящий примерно так:

"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 Ответ

1 голос
/ 12 апреля 2019
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"

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...