Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从字典列表中访问嵌套字典_Python_List_Dictionary_Json Normalize - Fatal编程技术网

Python 如何从字典列表中访问嵌套字典

Python 如何从字典列表中访问嵌套字典,python,list,dictionary,json-normalize,Python,List,Dictionary,Json Normalize,我有一个字典列表,很抱歉有点复杂,但我正在尝试显示真实数据: [{'alerts':[{'city':'city name1', ‘国家’:‘ZZ’, 'location':{'x':1,'y':3}, “米利斯”:1582337463000}, {'city':'city name2', ‘国家’:‘ZZ’, 'location':{'x':1,'y':3}, “pubMillis”:1582337573000, 'type':'TYPE2'}], “结束”:“11:02:00:000”, “

我有一个字典列表,很抱歉有点复杂,但我正在尝试显示真实数据:

[{'alerts':[{'city':'city name1', ‘国家’:‘ZZ’, 'location':{'x':1,'y':3}, “米利斯”:1582337463000}, {'city':'city name2', ‘国家’:‘ZZ’, 'location':{'x':1,'y':3}, “pubMillis”:1582337573000, 'type':'TYPE2'}], “结束”:“11:02:00:000”, “开始”:“11:01:00:000”}, {'alerts':[{'city':'city name3', ‘国家’:‘ZZ’, 'location':{'x':1,'y':3}, “米利斯”:1582337463000}], “结束”:“11:02:00:000”, “开始”:“11:01:00:000”}] 通常,列表结构如下所示:

[
{ [
    { {},
    },
    { {},
    }
  ],
},
{ [
    { {},
    },
    { {},
    }
  ],
}
]
如果我想访问城市名称1,我可以使用以下代码行进行访问:警报[0]['alerts'][0]['city']

如果我想访问城市名称2,我可以使用以下代码访问:警报[0]['alerts'][1]['city']

如何在循环中访问它?

使用嵌套循环:

其中警报等于目录列表 使用嵌套循环:

其中警报等于目录列表
目标是什么?要知道所有城市的名字

>>> for top_level_alert in alerts:
        for nested_alert in top_level_alert['alerts']:
            print(nested_alert['city'])
city name1
city name2
city name3

目标是什么?要知道所有城市的名字

>>> for top_level_alert in alerts:
        for nested_alert in top_level_alert['alerts']:
            print(nested_alert['city'])
city name1
city name2
city name3
使用熊猫 数据等于您的dicts示例列表 作为pd进口熊猫 创建数据帧并分解dict列表 df=pd.DataFramedata.explode'alerts'.reset\u indexdrop=True json_规范化dict并连接回df df=df.joinpd.json_normalizedf.alerts 删除“警报”列,因为不再需要它 df.dropcolumns=['alerts'],inplace=True 输出 起始-结束国家/城市milis location.x location.y类型pubMillis 0 11:01:00:000 11:02:00:000 ZZ城市名称1 1.582337e+12 1 3楠楠 1 11:01:00:000 11:02:00:000 ZZ城市名称2南1 3类型2 1.582338e+12 2 11:01:00:000 11:02:00:000 ZZ城市名称3 1.582337e+12 1 3楠楠 使用熊猫 数据等于您的dicts示例列表 作为pd进口熊猫 创建数据帧并分解dict列表 df=pd.DataFramedata.explode'alerts'.reset\u indexdrop=True json_规范化dict并连接回df df=df.joinpd.json_normalizedf.alerts 删除“警报”列,因为不再需要它 df.dropcolumns=['alerts'],inplace=True 输出 起始-结束国家/城市milis location.x location.y类型pubMillis 0 11:01:00:000 11:02:00:000 ZZ城市名称1 1.582337e+12 1 3楠楠 1 11:01:00:000 11:02:00:000 ZZ城市名称2南1 3类型2 1.582338e+12 2 11:01:00:000 11:02:00:000 ZZ城市名称3 1.582337e+12 1 3楠楠
好的,我想这不是我想要的。它可以访问一个环路中的所有城市。这不是你说的吗?好吧,我想这不是我想要的。它可以访问一个环路中的所有城市。这不是你说的吗?当我在实际数据集上应用时,它不起作用。它只捕获了第一行,我在你的数据结构上运行了它,它找到了所有3个城市的名称…这与我的想法给出的答案完全相同。当我应用于实际数据集时,它不起作用。它只捕获了第一行我在你的数据结构上运行了它,它找到了所有3个城市的名称…这与我的想法给出的答案完全相同。这正是我需要的!我的计划是把它们放在数据框中。很好,这可以满足其他字典中不可用的字典。访问嵌套字典可能是一个问题。很高兴这对你有用。这正是我需要的!我的计划是把它们放在数据框中。很好,这可以满足其他字典中不可用的字典。访问嵌套字典可能是一个问题。很高兴这对你有用。