如何在这个字典中只获取中间嵌套键?

zbsbpyhn  于 2021-09-08  发布在  Java
关注(0)|答案(1)|浏览(323)

我有以下字典:

{'AK': {'AK0010100': {'agency_name': 'Anchorage Police Department',
   'agency_type_name': 'City',
   'county_name': 'ANCHORAGE',
   'division_name': 'Pacific',
   'latitude': 61.17425,
   'longitude': -149.284329,
   'nibrs': False,
   'nibrs_start_date': None,
   'ori': 'AK0010100',
   'region_desc': 'Region IV',
   'region_name': 'West',
   'state_abbr': 'AK',
   'state_name': 'Alaska'},
  'AK0010200': {'agency_name': 'Fairbanks Police Department',
   'agency_type_name': 'City',
   'county_name': 'FAIRBANKS NORTH STAR',
   'division_name': 'Pacific',
   'latitude': 64.83945,
   'longitude': -147.71942,
   'nibrs': False,
   'nibrs_start_date': None,
   'ori': 'AK0010200',
   'region_desc': 'Region IV',
   'region_name': 'West',
   'state_abbr': 'AK',
   'state_name': 'Alaska'}}}

而我只需要辅助键(例如“ak0010100”)。我尝试了以下代码:

for keys, values in cities.items():
  for values in keys:
    print(keys[values])

但这似乎不起作用。我对python还比较陌生,所以如果有任何帮助,我将不胜感激。提前谢谢!

kyxcudwk

kyxcudwk1#

要获取辅助密钥,请执行以下操作:

dct = {
    "AK": {
        "AK0010100": {
            "agency_name": "Anchorage Police Department",
            "agency_type_name": "City",
            "county_name": "ANCHORAGE",
            "division_name": "Pacific",
            "latitude": 61.17425,
            "longitude": -149.284329,
            "nibrs": False,
            "nibrs_start_date": None,
            "ori": "AK0010100",
            "region_desc": "Region IV",
            "region_name": "West",
            "state_abbr": "AK",
            "state_name": "Alaska",
        },
        "AK0010200": {
            "agency_name": "Fairbanks Police Department",
            "agency_type_name": "City",
            "county_name": "FAIRBANKS NORTH STAR",
            "division_name": "Pacific",
            "latitude": 64.83945,
            "longitude": -147.71942,
            "nibrs": False,
            "nibrs_start_date": None,
            "ori": "AK0010200",
            "region_desc": "Region IV",
            "region_name": "West",
            "state_abbr": "AK",
            "state_name": "Alaska",
        },
    }
}

keys = [k for d in dct.values() for k in d]
print(keys)

印刷品:

['AK0010100', 'AK0010200']

相关问题