我正在尝试遍历嵌套字典的变量(JSON Google Maps输出)。下面的代码在较小的输出上工作,但是现在返回错误。
var geocode_result的长度为28376
。
lat_long_list = []
def geocode_results_process(x):
for i in range(len(x)):
list_component = x[i][0]['geometry']['location']
for val in list_component:
latitude = list_component['lat']
for val in list_component:
longitude = list_component['lng']
latlong = str(latitude) + ',' + str(longitude)
lat_long_list.append(latlong)
geocode_results_process(geocode_result)
预期结果是将纬度和经度的字符串列表附加到lat_long_list。
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-167-18aa2be2eddd> in <module>
13 lat_long_list.append(latlong)
14
---> 15 geocode_results_process(geocode_result)
<ipython-input-167-18aa2be2eddd> in geocode_results_process(x)
5 def geocode_results_process(x):
6 for i in range(len(x)):
----> 7 list_component = x[i][0]['geometry']['location']
8 for val in list_component:
9 latitude = list_component['lat']
IndexError: list index out of range
geocode_result示例:
geocode_result[1]
[{'address_components': [{'long_name': '620',
'short_name': '620',
'types': ['street_number']},
{'long_name': 'South Broadway',
'short_name': 'S Broadway',
'types': ['route']},
{'long_name': 'Downtown Los Angeles',
'short_name': 'Downtown Los Angeles',
'types': ['neighborhood', 'political']},
{'long_name': 'Los Angeles',
'short_name': 'Los Angeles',
'types': ['locality', 'political']},
{'long_name': 'Los Angeles County',
'short_name': 'Los Angeles County',
'types': ['administrative_area_level_2', 'political']},
{'long_name': 'California',
'short_name': 'CA',
'types': ['administrative_area_level_1', 'political']},
{'long_name': 'United States',
'short_name': 'US',
'types': ['country', 'political']},
{'long_name': '90014', 'short_name': '90014', 'types': ['postal_code']},
{'long_name': '1807',
'short_name': '1807',
'types': ['postal_code_suffix']}],
'formatted_address': '620 S Broadway, Los Angeles, CA 90014, USA',
'geometry': {'location': {'lat': 34.0459956, 'lng': -118.2523297},
'location_type': 'ROOFTOP',
'viewport': {'northeast': {'lat': 34.04734458029149,
'lng': -118.2509807197085},
'southwest': {'lat': 34.0446466197085, 'lng': -118.2536786802915}}},
'place_id': 'ChIJh_dVskrGwoARddqbvhmoZfg',
'plus_code': {'compound_code': '2PWX+93 Los Angeles, California, United States',
'global_code': '85632PWX+93'},
'types': ['street_address']}]
答案 0 :(得分:1)
我发现您编写的代码存在一些问题:
lat_long_list = []
应该在函数定义内。使用python,您可以通过执行以下操作使代码更具可读性:
def geocode_results_process(x):
lat_long_list = []
for list_item in x:
for item in list_item:
latitude= item['geometry']['location']['lat']
longitude = item['geometry']['location']['lat']
lat_long_list.append("{},{}".format(latitude, longitude))
我建议阅读此https://thispointer.com/python-how-to-unpack-list-tuple-or-dictionary-to-function-arguments-using/,以开始使用python中的拆箱列表
答案 1 :(得分:1)
好像您丢失了geocode_result
中的某些数据
使用
def geocode_results_process(x):
lat_long_list = []
for item in x:
for sub_item in item:
list_component = sub_item['geometry']['location']
latlong = str(list_component['lat']) + ',' + str(list_component['lng'])
lat_long_list.append(latlong)
return lat_long_list
注意:
range
。