从d的“list”中填充键值字典的“list”

2024-04-19 09:06:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个“巨大的”国家列表和与每个国家相关的数据,如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个是人口、语言和资本)。你知道吗


Tags: ofto数据innumpy列表fordictionary
2条回答

您可以使用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'}]

您想要:

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]))]

相关问题 更多 >