Python中的子集列表

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

从Google API查询中,我在Python 3.0中收到以下输出:

{'address_components': [{'long_name': '128',
    'short_name': '128',
    'types': ['street_number']},
    {'long_name': 'Mercedesstraße',
    'short_name': 'Mercedesstraße',
    'types': ['route']},
    {'long_name': 'Untertürkheim',
   'short_name': 'Untertürkheim',
   'types': ['political', 'sublocality', 'sublocality_level_1']},
   {'long_name': 'Stuttgart',
   'short_name': 'Stuttgart',
   'types': ['locality', 'political']},
   {'long_name': 'Stuttgart',
   'short_name': 'Süd',
   'types': ['administrative_area_level_2', 'political']},
   {'long_name': 'Baden-Württemberg',
   'short_name': 'BW',
   'types': ['administrative_area_level_1', 'political']},
   {'long_name': 'Germany',
   'short_name': 'DE',
   'types': ['country', 'political']},
   {'long_name': '70327', 'short_name': '70327', 'types': ['postal_code']}],
   'formatted_address': 'Mercedesstraße 128, 70327 Stuttgart, Germany',
   'geometry': {'location': {'lat': 48.7863462, 'lng': 9.2380718},
   'location_type': 'ROOFTOP',
   'viewport': {'northeast': {'lat': 48.7876951802915,
    'lng': 9.239420780291503},
   'southwest': {'lat': 48.7849972197085, 'lng': 9.236722819708499}}},
   'place_id': 'ChIJr3KQnVbEmUcReuFmYm-eFkI',
   'types': ['establishment', 'point_of_interest']}

我想访问纬度/经度代码'geometry':{'location':{'lat':48.7863462,'lng':9.2380718},但我无法使用Python中的常规列表索引方法。任何帮助都非常感谢。

对于Type I得到“dict”:

类型(geocode_result [0])

并为

类型(geocode_result)

我得到“名单”。

python-3.x list google-api
1个回答
1
投票

正确格式化:

d = {'address_components': 
    [
        { 'long_name': '128', 'short_name': '128', 'types': ['street_number'] },
        { 'long_name': 'Mercedesstraße', 'short_name': 'Mercedesstraße', 'types': ['route'] },
        { 'long_name': 'Untertürkheim', 'short_name': 'Untertürkheim', 'types': ['political', 'sublocality', 'sublocality_level_1'] },
        { 'long_name': 'Stuttgart', 'short_name': 'Stuttgart', 'types': ['locality', 'political'] },
        { 'long_name': 'Stuttgart', 'short_name': 'Süd', 'types': ['administrative_area_level_2', 'political'] },
        { 'long_name': 'Baden-Württemberg', 'short_name': 'BW', 'types': ['administrative_area_level_1', 'political'] },
        { 'long_name': 'Germany', 'short_name': 'DE', 'types': ['country', 'political'] },
        { 'long_name': '70327', 'short_name': '70327', 'types': ['postal_code'] }
    ],
   'formatted_address': 'Mercedesstraße 128, 70327 Stuttgart, Germany',
   'geometry': {'location': {'lat': 48.7863462, 'lng': 9.2380718}, 
                'location_type': 'ROOFTOP',
                'viewport': 
                    {
                        'northeast': {'lat': 48.7876951802915, 'lng': 9.239420780291503}, 
                        'southwest': {'lat': 48.7849972197085, 'lng': 9.236722819708499}
                    }
               },
   'place_id': 'ChIJr3KQnVbEmUcReuFmYm-eFkI',
   'types': ['establishment', 'point_of_interest']
}

然后:

print (d['geometry']['location']) 

输出:

{'lat': 48.7863462, 'lng': 9.2380718}

或者,如果您想直接解决它们:

lat = d['geometry']['location']['lat']   
lng = d['geometry']['location']['lng'] 
© www.soinside.com 2019 - 2024. All rights reserved.