如何在python中循环浏览大型嵌套列表?

问题描述 投票:0回答:1

我了解如何遍历单个列表和单个数组。但是,我有一个嵌套列表,并希望在[[same索引中的每个嵌套列表中的第一个元素后附加lon,然后再移至下一个索引并重复该过程,直到已应用所有索引为止。

0 [[[-105.077274, 40.514625], [-105.077005, 40.5... 1 [[[-105.024284, 40.509791], [-105.024274, 40.5... 2 [[[-105.087928, 40.578187], [-105.087939, 40.5... 3 [[[-105.11976, 40.589318], [-105.11977, 40.587... 4 [[[-105.083718, 40.568761], [-105.08459, 40.56... ... 995 [[[-105.05362, 40.525161], [-105.053607, 40.52... 996 [[[-105.030003, 40.62114], [-105.030012, 40.62... 997 [[[-105.123316, 40.560645], [-105.123353, 40.5... 998 [[[-105.070162, 40.580083], [-105.070175, 40.5... 999 [[[-105.120617, 40.560044], [-105.120637, 40.5... Name: geometry_coordinates, Length: 1000, dtype: object
[当前,我的代码在第一个列表/元素lon中将index 0附加到-105.077274,但不是停留在index 0中,而是循环到index 1并接着附加-105.024284

因此,当前lon看起来像lon=[-105.077274,-105.024284...],但我试图让它先附加0 index,就像lon=[-105.077274,-105.077005...]一样,然后下移至index 1

import json import pandas as pd from pandas.io.json import json_normalize geojson = json.load(open("Data/Lanes.geojson")) geojson = json_normalize(geojson['features'], sep="_") print(geojson['geometry_coordinates']) lon = [] lat = [] for longitude in geojson['geometry_coordinates']: lon.append(longitude[0][0][0])

任何帮助,谢谢。
python list loops append
1个回答
1
投票
使用嵌套循环。

for l1 in geojson['geometry_coordinates']: for l2 in l1: for l3 in l2: lon.append(l3[0]) lat.append(l3[1])

© www.soinside.com 2019 - 2024. All rights reserved.