我有两个包含字典的列表:
list105 = [
{'Country': 'Zimbabwe', 'GDP/Pop 2005': 281.0751453319367}
{'Country': 'Zambia', 'GDP/Pop 2005': 654.055392253311}
{'Country': 'Congo (Dem. Rep.)', 'GDP/Pop 2005': 115.37122637190915}
]
list202 = [
{'Country': 'Vietnam', 'GDP/Pop 2002': 633.4709249146734}
{'Country': 'Zambia', 'GDP/Pop 2002': 1198.4556066429468}
{'Country': 'Vanuatu', 'GDP/Pop 2002': 1788.4344216880352}
]
是否可以遍历两个字典列表,匹配“国家”键,并将任一字典中的所有唯一键附加到在第三个列表中创建的新字典中?例如,从上面开始,第三个列表将包含:
list2and3 = [
{'Country': 'Zambia', 'GDP/Pop 2005': 654.055392253311, 'GDP/Pop 2002': 1198.4556066429468}
]
我从以下内容开始:
list2and3 = []
for line in list105:
for row in list202:
if line['Country'] == row['Country']:
#do something and append to list2and3