如何从索引中检索列表中的特定词典?

时间:2020-08-06 01:51:04

标签: python list dictionary

我在词典中有字典列表。以下是列表中前两个嵌套词典的显示方式示例:

{
"type": "FeatureCollection",
"name": "waypoints_geoJSONtest",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "fid": 1.0, "point": "001", "time": "2020\/03\/05 16:17:11.000", "elevation": 68.171204, "notes_plan": null, "notes_lulc": null, "notes_medi": null, "notes_othe": "Inicio trayecto", "Category": null }, "geometry": { "type": "Point", "coordinates": [ -78.759113, 9.034811 ] } },
{ "type": "Feature", "properties": { "fid": 2.0, "point": "002", "time": "2020\/03\/05 16:22:53.000", "elevation": 76.204994, "notes_plan": "Cuipo", "notes_lulc": null, "notes_medi": null, "notes_othe": null, "Category": "Plantas" }, "geometry": { "type": "Point", "coordinates": [ -78.759178, 9.034232 ] } },

如您所见,“功能”包含字典列表(从第5行开始)。我遇到的麻烦是通过索引访问了第一个字典。我已经尝试过了:

for listcontents in data["features"]:
    print(listcontents[1])

哪个会回来

KeyError: 1

我对为什么不能仅仅基于索引调用字典感到困惑,因为它位于列表中。我知道字典是无序的,但是由于字典在有序列表中,所以我难道不应该只根据列表中的位置来调用字典吗?

1 个答案:

答案 0 :(得分:0)

布伦达·汤普森(Brenda Thompson),我刚刚对数据进行了一些更改,但缺少了一些右括号,希望您对这个解决方案有帮助:)

data = {
            "type": "FeatureCollection",
            "name": "waypoints_geoJSONtest",
            "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }},
            "features": [{ "type": "Feature", "properties": { "fid": 1.0, "point": "001", "time": "2020\/03\/05 16:17:11.000", "elevation": 68.171204, "notes_plan": None, "notes_lulc": None, "notes_medi": None, "notes_othe": "Inicio trayecto", "Category": None }, "geometry": { "type": "Point", "coordinates": [ -78.759113, 9.034811 ] } },{ "type": "Feature", "properties": { "fid": 2.0, "point": "002", "time": "2020\/03\/05 16:22:53.000", "elevation": 76.204994, "notes_plan": "Cuipo", "notes_lulc": None, "notes_medi": None, "notes_othe": None, "Category": "Plantas" }, "geometry": { "type": "Point", "coordinates": [ -78.759178, 9.034232 ] } }],
        }
for i in data["features"]:
    print(i)

输出:

{'type': 'Feature', 'properties': {'fid': 1.0, 'point': '001', 'time': '2020\\/03\\/05 16:17:11.000', 'elevation': 68.171204, 'notes_plan': 
None, 'notes_lulc': None, 'notes_medi': None, 'notes_othe': 'Inicio trayecto', 'Category': None}, 'geometry': {'type': 'Point', 'coordinates': [-78.759113, 9.034811]}}
{'type': 'Feature', 'properties': {'fid': 2.0, 'point': '002', 'time': '2020\\/03\\/05 16:22:53.000', 'elevation': 76.204994, 'notes_plan': 
'Cuipo', 'notes_lulc': None, 'notes_medi': None, 'notes_othe': None, 'Category': 'Plantas'}, 'geometry': {'type': 'Point', 'coordinates': [-78.759178, 9.034232]}}

根据要求,如果您需要按索引访问数据,请使用此代码

data = {
            "type": "FeatureCollection",
            "name": "waypoints_geoJSONtest",
            "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" }},
            "features": [{ "type": "Feature", "properties": { "fid": 1.0, "point": "001", "time": "2020\/03\/05 16:17:11.000", "elevation": 68.171204, "notes_plan": None, "notes_lulc": None, "notes_medi": None, "notes_othe": "Inicio trayecto", "Category": None }, "geometry": { "type": "Point", "coordinates": [ -78.759113, 9.034811 ] } },{ "type": "Feature", "properties": { "fid": 2.0, "point": "002", "time": "2020\/03\/05 16:22:53.000", "elevation": 76.204994, "notes_plan": "Cuipo", "notes_lulc": None, "notes_medi": None, "notes_othe": None, "Category": "Plantas" }, "geometry": { "type": "Point", "coordinates": [ -78.759178, 9.034232 ] } }],
        }
for i in range(len(data["features"])):
    print(data["features"][i])