Как получить конкретный словарь в списке из индекса? - PullRequest
0 голосов
/ 06 августа 2020

У меня есть список словарей в словаре. Вот пример того, как появляются первые два вложенных словаря в списке:

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

Как видите, "features" содержит список словарей (начиная со строки 5). У меня проблемы с доступом к первому словарю через индексацию. Я пробовал это:

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

Что просто возвращает

KeyError: 1

Я не понимаю, почему я не могу просто вызвать словарь на основе индекса, поскольку он находится в списке. Я знаю, что словари неупорядочены, но поскольку словари находятся в упорядоченном списке, разве я не могу просто вызвать dict на основе позиции в списке?

1 Ответ

0 голосов
/ 17 августа 2020

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])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...