将字典理解与列表理解相结合以合并字典对
Combine dict comprehension with list comprehension to merge dict pairs
给定字典 'countries':
countries = [
{'country': 'Aruba', 'iso3': 'ABW'},
{'country': 'Afghanistan', 'iso3': 'AFG'},
{'country': 'Angola', 'iso3': 'AGO'},
{'country': 'Anguilla', 'iso3': 'AIA'},
{'country': 'Albania', 'iso3': 'ALB'},
{'country': 'Andorra', 'iso3': 'AND'},
{'country': 'United Arab Emirates', 'iso3': 'ARE'},
{'country': 'Argentina', 'iso3': 'ARG'},
{'country': 'Armenia', 'iso3': 'ARM'},
{'country': 'American Samoa', 'iso3': 'ASM'}
]
我想将 iso3 值作为键,将国家/地区值作为值。期望的输出:
[
{'ABW': 'Aruba'},
{'AFG': 'Afghanistan'},
{'AGO': 'Angola'},
{'AIA': 'Anguilla'},
{'ALB': 'Albania'},
{'AND': 'Andorra'},
{'ARE': 'United Arab Emirates'},
{'ARG': 'Argentina'},
{'ARM': 'Armenia'},
{'ASM': 'American Samoa'}
]
我通过以下方式实现了它:
iso3_map = []
for country in countries:
data = {
country['iso3']: country['country']
}
iso3_map.append(data)
但我想知道如何在列表理解中以更优雅的方式做到这一点。
我尝试了一些方法,最接近的是:
[{v if k == 'iso3' else None: v if k == 'country' else None for k, v in country.items()} for country in countries]
但是 return 没有想要的输出
使用
iso3_map = [{x['iso3']: x['country']} for x in countries]
输出
[{'ABW': 'Aruba'},
{'AFG': 'Afghanistan'},
{'AGO': 'Angola'},
{'AIA': 'Anguilla'},
{'ALB': 'Albania'},
{'AND': 'Andorra'},
{'ARE': 'United Arab Emirates'},
{'ARG': 'Argentina'},
{'ARM': 'Armenia'},
{'ASM': 'American Samoa'}]
这应该会为您提供所需的输出。
[{country['iso3']: country['country']} for country in countries]
对于您提供的数据,您只需引用每个输入字典中的 iso3
和 country
值即可为结果中的每个字典创建 key/value 对:
data = [{ ctry['iso3'] : ctry['country'] } for ctry in countries]
输出:
[
{'ABW': 'Aruba'},
{'AFG': 'Afghanistan'},
{'AGO': 'Angola'},
{'AIA': 'Anguilla'},
{'ALB': 'Albania'},
{'AND': 'Andorra'},
{'ARE': 'United Arab Emirates'},
{'ARG': 'Argentina'},
{'ARM': 'Armenia'},
{'ASM': 'American Samoa'}
]
给定字典 'countries':
countries = [
{'country': 'Aruba', 'iso3': 'ABW'},
{'country': 'Afghanistan', 'iso3': 'AFG'},
{'country': 'Angola', 'iso3': 'AGO'},
{'country': 'Anguilla', 'iso3': 'AIA'},
{'country': 'Albania', 'iso3': 'ALB'},
{'country': 'Andorra', 'iso3': 'AND'},
{'country': 'United Arab Emirates', 'iso3': 'ARE'},
{'country': 'Argentina', 'iso3': 'ARG'},
{'country': 'Armenia', 'iso3': 'ARM'},
{'country': 'American Samoa', 'iso3': 'ASM'}
]
我想将 iso3 值作为键,将国家/地区值作为值。期望的输出:
[
{'ABW': 'Aruba'},
{'AFG': 'Afghanistan'},
{'AGO': 'Angola'},
{'AIA': 'Anguilla'},
{'ALB': 'Albania'},
{'AND': 'Andorra'},
{'ARE': 'United Arab Emirates'},
{'ARG': 'Argentina'},
{'ARM': 'Armenia'},
{'ASM': 'American Samoa'}
]
我通过以下方式实现了它:
iso3_map = []
for country in countries:
data = {
country['iso3']: country['country']
}
iso3_map.append(data)
但我想知道如何在列表理解中以更优雅的方式做到这一点。 我尝试了一些方法,最接近的是:
[{v if k == 'iso3' else None: v if k == 'country' else None for k, v in country.items()} for country in countries]
但是 return 没有想要的输出
使用
iso3_map = [{x['iso3']: x['country']} for x in countries]
输出
[{'ABW': 'Aruba'},
{'AFG': 'Afghanistan'},
{'AGO': 'Angola'},
{'AIA': 'Anguilla'},
{'ALB': 'Albania'},
{'AND': 'Andorra'},
{'ARE': 'United Arab Emirates'},
{'ARG': 'Argentina'},
{'ARM': 'Armenia'},
{'ASM': 'American Samoa'}]
这应该会为您提供所需的输出。
[{country['iso3']: country['country']} for country in countries]
对于您提供的数据,您只需引用每个输入字典中的 iso3
和 country
值即可为结果中的每个字典创建 key/value 对:
data = [{ ctry['iso3'] : ctry['country'] } for ctry in countries]
输出:
[
{'ABW': 'Aruba'},
{'AFG': 'Afghanistan'},
{'AGO': 'Angola'},
{'AIA': 'Anguilla'},
{'ALB': 'Albania'},
{'AND': 'Andorra'},
{'ARE': 'United Arab Emirates'},
{'ARG': 'Argentina'},
{'ARM': 'Armenia'},
{'ASM': 'American Samoa'}
]