访问词典列表中的值

时间:2019-07-14 23:17:32

标签: python

Python的新手,并使用Kaggle的LA Parking Citation数据库进行练习。

我从GoogleMaps API检索了数据,现在我正努力解析它。有点背景,我在数据框中有一个地址列表,将它们通过API放置,然后将结果放置在名为geocode_result的变量中。我想从该变量访问相应的邮政编码,将其放入列表格式,然后附加我的数据框以进行进一步分析。

我想在字典geocode_result列表中创建所有邮政编码的列表。现在,邮政编码返回一个空列表。

list_component = geocode_result[0][0]['geometry']['location']['address_components']
zipcodes = []

def geocode_results_process(list_component):        
    for dct in list_component:
        if 'postal_code' in dct['types']:
            zipcode = dct['short_name']
            zipcodes.append(zipcode)

# - this returns a null list

下面,我已经漂亮地打印了geocode_result的第一个元素。我想要的元素是“邮政编码”>“短名称”>“ 90057”。

pprint(geocode_result[0])

[{'address_components': [{'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']}],
  'formatted_address': 'Los Angeles, CA, USA',
  'geometry': {'bounds': {'northeast': {'lat': 34.3373061, 'lng': -118.1552891},
                          'southwest': {'lat': 33.7036519,
                                        'lng': -118.6681759}},
               'location': {'address_components': [{'long_name': '721',
                                                    'short_name': '721',
                                                    'types': ['street_number']},
                                                   {'long_name': 'South '
                                                                 'Westlake '
                                                                 'Avenue',
                                                    'short_name': 'S Westlake '
                                                                  'Ave',
                                                    'types': ['route']},
                                                   {'long_name': 'Westlake',
                                                    'short_name': 'Westlake',
                                                    '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': '90057',
                                                    'short_name': '90057',
                                                    'types': ['postal_code']}],
                            'formatted_address': '721 S Westlake Ave, Los '
                                                 'Angeles, CA 90057, USA',
                            'geometry': {'bounds': {'northeast': {'lat': 34.0555963,
                                                                  'lng': -118.2761535},
                                                    'southwest': {'lat': 34.0553198,
                                                                  'lng': -118.2766184}},
                                         'location': {'lat': 34.0554475,
                                                      'lng': -118.2763928},
                                         'location_type': 'ROOFTOP',
                                         'viewport': {'northeast': {'lat': 34.05680703029149,
                                                                    'lng': -118.2750369697085},
                                                      'southwest': {'lat': 34.0541090697085,
                                                                    'lng': -118.2777349302915}}},
                            'place_id': 'ChIJHbSDWZ_HwoARj7BGtBrQq94',
                            'types': ['premise']},
               'location_type': 'APPROXIMATE',
               'viewport': {'northeast': {'lat': 34.3373061,
                                          'lng': -118.1552891},
                            'southwest': {'lat': 33.7036519,
                                          'lng': -118.6681759}}},
  'place_id': 'ChIJE9on3F3HwoAR9AhGJW_fL-I',
  'types': ['locality', 'political']}]

这是我细分的内容:

pprint(geocode_result[0][0]['geometry']['location']['address_components])

[{'long_name': '721', 'short_name': '721', 'types': ['street_number']},
 {'long_name': 'South Westlake Avenue',
  'short_name': 'S Westlake Ave',
  'types': ['route']},
 {'long_name': 'Westlake',
  'short_name': 'Westlake',
  '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': '90057', 'short_name': '90057', 'types': ['postal_code']}]

我想以编程方式遍历我的变量geocode_results来创建邮政编码列表。

1 个答案:

答案 0 :(得分:1)

您的代码对我来说很好

geocode_result = [[{'address_components': [{'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']}],
                 'formatted_address': 'Los Angeles, CA, USA',
                 'geometry': {'bounds': {'northeast': {'lat': 34.3373061, 'lng': -118.1552891},
                                         'southwest': {'lat': 33.7036519,
                                                       'lng': -118.6681759}},
                              'location': {'address_components': [{'long_name': '721',
                                                                   'short_name': '721',
                                                                   'types': ['street_number']},
                                                                  {'long_name': 'South '
                                                                                'Westlake '
                                                                                'Avenue',
                                                                   'short_name': 'S Westlake '
                                                                                 'Ave',
                                                                   'types': ['route']},
                                                                  {'long_name': 'Westlake',
                                                                   'short_name': 'Westlake',
                                                                   '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': '90057',
                                                                   'short_name': '90057',
                                                                   'types': ['postal_code']}],
                                           'formatted_address': '721 S Westlake Ave, Los '
                                                                'Angeles, CA 90057, USA',
                                           'geometry': {'bounds': {'northeast': {'lat': 34.0555963,
                                                                                 'lng': -118.2761535},
                                                                   'southwest': {'lat': 34.0553198,
                                                                                 'lng': -118.2766184}},
                                                        'location': {'lat': 34.0554475,
                                                                     'lng': -118.2763928},
                                                        'location_type': 'ROOFTOP',
                                                        'viewport': {'northeast': {'lat': 34.05680703029149,
                                                                                   'lng': -118.2750369697085},
                                                                     'southwest': {'lat': 34.0541090697085,
                                                                                   'lng': -118.2777349302915}}},
                                           'place_id': 'ChIJHbSDWZ_HwoARj7BGtBrQq94',
                                           'types': ['premise']},
                              'location_type': 'APPROXIMATE',
                              'viewport': {'northeast': {'lat': 34.3373061,
                                                         'lng': -118.1552891},
                                           'southwest': {'lat': 33.7036519,
                                                         'lng': -118.6681759}}},
                 'place_id': 'ChIJE9on3F3HwoAR9AhGJW_fL-I',
                 'types': ['locality', 'political']}]]

list_component = geocode_result[0][0]['geometry']['location']['address_components']
zipcodes = []

def geocode_results_process(list_component):
    for dct in list_component:
        if 'postal_code' in dct['types']:
            zipcode = dct['short_name']
            zipcodes.append(zipcode)

geocode_results_process(list_component)
print(zipcodes)

>>> ['90057']

基于您发布的代码,我怀疑您没有调用您创建的函数?

更新:遍历geocode_result

for el in geocode_result:
    for sub_el in el:
        geocode_results_process(sub_el['geometry']['location']['address_components'])