我有一个“巨大的”国家列表和与每个国家相关的数据,如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个是人口、语言和资本)。你知道吗
您可以使用
zip
例如:
或者
输出:
您想要:
相关问题 更多 >
编程相关推荐