从数据“列表”中填充键值字典的“列表”

时间:2019-06-04 09:19:00

标签: python dictionary

我有一个“巨大”的国家/地区列表,以及与每个国家/地区相关的数据(如python中的列表)。我需要将数据映射到每个列表。因此,在进行一些谷歌搜索之后,已开始为此目的在python中使用字典。也就是说,每个国家/地区的每本词典都会产生一个词典列表。然后,使用大写,语言和人口等数据(键)及其相应的值填充这些词典。最终,我应该将它们全部设置为numpy数组,因为我需要绘制示例,国家和人口图。

我只是一个初学者,所以请随时提出任何更好的方法。这就是我的开始(将我的字典减少到只有4个),并以某种方式陷入了困境。

#!/usr/bin/python
#
#Dictionary example
#


import numpy as np

if __name__ == '__main__':

    country_names = [ 'Germany', 'Italy', 'Netherlands', 'France'] #list of countries

    capital_list =['Berlin', 'Rome', 'Amsterdam', 'Paris']#list of capitals to be mapped to countries
    population_list= ['3.5','2.1', '0.3', '2.8'] #list of population of induvidual countries, all figures in Million
    language_list=['German','Italian','Dutch','French'] #list of languages to be mapped to countries

    #dict1={"CAPITAL":xxx, "POPULATION":yyy, "LANGUAGE": zzz} ##map these data as key value pairs for each country(where each country is a      dictionary)
    dictionary_list ={ "CAPITAL": i, "POPULATION":i, "LANGUAGE": i for i in country_names}
    #dictionary_list ={ i: [] for i in country_names}

    for i in range(len(country_names)): 
        dictionary_list['CAPITAL'] = capital_list[i]
        dictionary_list['POPULATION'] = population_list[i]
        dictionary_list['LANGUAGE'] = language_list[i]
    print dictionary_list
    #for value in dict.itervalues(): ...

    #for key, value in dict.iteritems():

    #numpy array of dictionary

我的问题是我看不到如何交互式地创建字典并用其值迭代地填充键。唯一固定的指标是关键(目前只有3个指标是人口,语言和资本)。

2 个答案:

答案 0 :(得分:1)

您可以使用zip

例如:

country_names = [ 'Germany', 'Italy', 'Netherlands', 'France'] #list of countries
capital_list =['Berlin', 'Rome', 'Amsterdam', 'Paris']#list of capitals to be mapped to countries
population_list= ['3.5','2.1', '0.3', '2.8'] #list of population of induvidual countries, all figures in Million
language_list=['German','Italian','Dutch','French'] #list of languages to be mapped to countries

dictionary_list = [ {"COUNTRY": country, "CAPITAL": capital, "POPULATION":population, "LANGUAGE": lang} for country, capital, population, lang in zip(country_names, capital_list, population_list, language_list)]
print(dictionary_list)

keys = ["COUNTRY", "CAPITAL", "POPULATION", "LANGUAGE"]
dictionary_list = [ dict(zip(keys,data)) for data in zip(country_names, capital_list, population_list, language_list)]
print(dictionary_list)

输出:

[{'CAPITAL': 'Berlin',
  'COUNTRY': 'Germany',
  'LANGUAGE': 'German',
  'POPULATION': '3.5'},
 {'CAPITAL': 'Rome',
  'COUNTRY': 'Italy',
  'LANGUAGE': 'Italian',
  'POPULATION': '2.1'},
 {'CAPITAL': 'Amsterdam',
  'COUNTRY': 'Netherlands',
  'LANGUAGE': 'Dutch',
  'POPULATION': '0.3'},
 {'CAPITAL': 'Paris',
  'COUNTRY': 'France',
  'LANGUAGE': 'French',
  'POPULATION': '2.8'}]

答案 1 :(得分:1)

您想要吗?

headers = ['name' , 'capital' , 'language']

arrs = [country_names , population_list, language_list]


dicts = [   { name: arr[x] for name, arr in zip(headers, arrs )} for x in range(len(country_names [0]))]