如何在python中访问字典下的字典

如何在python中访问字典下的字典,python,json,dictionary,jupyter-notebook,Python,Json,Dictionary,Jupyter Notebook,`示例假设这是一个json字典- 如何将其编入下面所示的词典 dictionary={ "actions": [ { "action_type": "comment", "value": "1" }, { "action_type": "like", "value": "18" }, { "action_type": "post", "value": "4" }, { "action_type": "post_reaction", "value": "268" }, { "action_ty

`示例假设这是一个json字典-

如何将其编入下面所示的词典

dictionary={
"actions": [
{
"action_type": "comment",
 "value": "1"
},
{
"action_type": "like",
"value": "18"
},
{
"action_type": "post",
"value": "4"
},
{
"action_type": "post_reaction",
 "value": "268"
},
{
"action_type": "video_view",
"value": "198245"
},
{
"action_type": "page_engagement",
"value": "198536"
},
{
"action_type": "post_engagement",
"value": "198518"
}
],
"date_start": "2018-03-30",
"date_stop": "2018-03-30"
}
我尝试了迭代器、生成函数和其他方法,但现在我被卡住了

dictionary={
"comment": "1"
"like": "18"
"post": "4"
"post_reaction": "268"
"video_view": "198245"
"page_engagement": "198536"
"post_engagement": "198518"
"date_start": "2018-03-30",
"date_stop": "2018-03-30"
}`

迭代
操作
键并创建所需的输出:

Ex:

d = {}
for i in data["actions"]:
    d[i["action_type"]] = i["value"] 

d.update({"date_start": data["date_start"], "date_stop": data["date_start"]})
print(d)
{'comment': '1', 'date_stop': '2018-03-30', 'like': '18', 'date_start': '2018-03-30', 'post_engagement': '198518', 'page_engagement': '198536', 'video_view': '198245', 'post_reaction': '268', 'post': '4'}
{'comment': '1',
 'date_start': '2018-03-30',
 'date_stop': '2018-03-30',
 'like': '18',
 'page_engagement': '198536',
 'post': '4',
 'post_engagement': '198518',
 'post_reaction': '268',
 'video_view': '198245'}
或听写理解

print dict((i["action_type"], i["value"]) for i in data["actions"])
输出:

d = {}
for i in data["actions"]:
    d[i["action_type"]] = i["value"] 

d.update({"date_start": data["date_start"], "date_stop": data["date_start"]})
print(d)
{'comment': '1', 'date_stop': '2018-03-30', 'like': '18', 'date_start': '2018-03-30', 'post_engagement': '198518', 'page_engagement': '198536', 'video_view': '198245', 'post_reaction': '268', 'post': '4'}
{'comment': '1',
 'date_start': '2018-03-30',
 'date_stop': '2018-03-30',
 'like': '18',
 'page_engagement': '198536',
 'post': '4',
 'post_engagement': '198518',
 'post_reaction': '268',
 'video_view': '198245'}

您可以通过迭代整个字典来实现这一点。如果类型是一个列表,则应该迭代该列表。这样做还将允许具有类似布局的其他词典(其他词典的列表或字符串)由同一例程处理

output_dict = {}
#for key, value in input_dict.items(): python 3
for key, value in input_dict.iteritems(): # python 2
      if type(value) == list:
            for item in value:
                  for key2 in item:
                        if key2 != 'value':
                              output_dict[item[key2]] = item['value']
      else:
            output_dict[key] = value
这将产生:

{'comment': '1',
 'date_start': '2018-03-30',
 'date_stop': '2018-03-30',
 'like': '18',
 'page_engagement': '198536',
 'post': '4',
 'post_engagement': '198518',
 'post_reaction': '268',
 'video_view': '198245'}
试试看 使用iteritems()并检查值是否为列表

res = {}
for key,value in dict.iteritems():
if isinstance(value,list):
    for item in value:
        res[item['action_type']] = item['value']
else:
    res[key] = value
输出:

d = {}
for i in data["actions"]:
    d[i["action_type"]] = i["value"] 

d.update({"date_start": data["date_start"], "date_stop": data["date_start"]})
print(d)
{'comment': '1', 'date_stop': '2018-03-30', 'like': '18', 'date_start': '2018-03-30', 'post_engagement': '198518', 'page_engagement': '198536', 'video_view': '198245', 'post_reaction': '268', 'post': '4'}
{'comment': '1',
 'date_start': '2018-03-30',
 'date_stop': '2018-03-30',
 'like': '18',
 'page_engagement': '198536',
 'post': '4',
 'post_engagement': '198518',
 'post_reaction': '268',
 'video_view': '198245'}

请添加您的代码以显示您已尝试的内容。如果您不共享,我们将无法修复您的代码。您好!!它真的很有用,但我在facebookads.api.Cursor中使用了它。我不能重复它。那么,您知道如何在本文中为facebookads.api.Cursor使用for循环吗