Python 从列表中获取字典值
我正在使用GoogleGeocache从一个地址获取纬度和经度。它输出以下内容Python 从列表中获取字典值,python,list,dictionary,Python,List,Dictionary,我正在使用GoogleGeocache从一个地址获取纬度和经度。它输出以下内容 {'address_components': [{'long_name': '101', 'short_name': '101', 'types': ['street_number']}, {'long_name': 'Oak Street', 'short_name': 'Oak St', 'types': ['route']}, {'long_name': 'Hayes Valley', 'short_nam
{'address_components': [{'long_name': '101', 'short_name': '101', 'types': ['street_number']}, {'long_name': 'Oak Street', 'short_name': 'Oak St', 'types': ['route']}, {'long_name': 'Hayes Valley', 'short_name': 'Hayes Valley', 'types': ['neighborhood', 'political']}, {'long_name': 'San Francisco', 'short_name': 'SF', 'types': ['locality', 'political']}, {'long_name': 'San Francisco County', 'short_name': 'San Francisco 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': '94102', 'short_name': '94102', 'types': ['postal_code']}], 'formatted_address': '101 Oak St, San Francisco, CA 94102, USA', 'geometry': {'bounds': {'northeast': {'lat': 37.77513769999999, 'lng': -122.4210453}, 'southwest': {'lat': 37.7750128, 'lng': -122.4211758}}, 'location': {'lat': 37.7750683, 'lng': -122.4210876}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat': 37.7764242302915, 'lng': -122.4197615697085}, 'southwest': {'lat': 37.7737262697085, 'lng': -122.4224595302915}}}, 'partial_match': True, 'place_id': 'ChIJZQRo4J6AhYARbQ70tvpMUfE', 'types': ['subpremise']}
如果我们假设它存储在一个变量中,那么如何使用python获得纬度和经度呢
我希望从中获得以下经度和纬度,并将其存储在一个变量中:
location': {'lat': 37.7750683, 'lng': -122.4210876}
谢谢 让
d
成为您发布的词典,然后
loc=d[“几何体”][“位置”]
lat,lng=loc[“lat”],loc[“lng”]
上面的代码根据您的问题提供了您想要的输出。最后,我将所有位置添加到一个列表中。请查看我编写的代码。请浏览、和,了解该网站的工作原理,并帮助您改进当前和未来的问题,从而帮助您获得更好的答案。“演示如何解决此编码问题?”与堆栈溢出无关。您必须诚实地尝试解决方案,然后询问有关实现的特定问题。堆栈溢出并不是为了替换现有的教程和文档。获取列表元素和从dict中获取值是可以在任何教程中找到的关于每种数据类型的介绍性项目。这是否回答了您的问题?
your_provided_dict = {'address_components': [{'long_name': '101',
'short_name': '101', 'types': ['street_number']}, {'long_name': 'Oak Street',
'short_name': 'Oak St', 'types': ['route']}, {'long_name': 'Hayes Valley',
'short_name': 'Hayes Valley', 'types': ['neighborhood', 'political']},
{'long_name': 'San Francisco', 'short_name': 'SF', 'types': ['locality',
'political']}, {'long_name': 'San Francisco County', 'short_name': 'San
Francisco 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': '94102',
'short_name': '94102', 'types': ['postal_code']}], 'formatted_address': '101
Oak St, San Francisco, CA 94102, USA', 'geometry': {'bounds': {'northeast':
{'lat': 37.77513769999999, 'lng': -122.4210453}, 'southwest': {'lat':
37.7750128, 'lng': -122.4211758}}, 'location': {'lat': 37.7750683, 'lng':
-122.4210876}, 'location_type': 'ROOFTOP', 'viewport': {'northeast': {'lat':
37.7764242302915, 'lng': -122.4197615697085}, 'southwest': {'lat':
37.7737262697085, 'lng': -122.4224595302915}}}, 'partial_match': True,
'place_id': 'ChIJZQRo4J6AhYARbQ70tvpMUfE', 'types': ['subpremise']}
your_provided_dict_bounds = test_d['geometry']['bounds']
location_list = []
for key,value in your_provided_dict_bounds.items():
location = "location:"+str(value)
location_list.append(location)
print(location_list)