所以我需要帮助循环通过一个嵌套的字典,我已经创建,以回答一些问题。我的代码将两个不同的字典拆分并向其中添加项,如下所示: 链接到csv: https://docs.google.com/document/d/1v68_QQX7Tn96l-b0LMO9YZ4ZAn_KWDMUJboa6LEyPr8/edit?usp=sharing
import csv
region_data = {}
country_data = {}
answers = []
data = []
cuntry = False
f = open('dph_SYB60_T03_Population Growth, Fertility and Mortality Indicators.csv')
reader = csv.DictReader(f)
for line in reader:
#This gets all the values into a standard dict
data.append(dict(line))
#This will loop thru the dict and create variables to hold specific items
for i in data:
# collects all of the Region/Country/Area
location = i['Region/Country/Area']
# Gets All the Years
years = i['Year']
i_d = i['ID']
info = i['Footnotes']
series = i['Series']
value = float(i['Value'])
# print(series)
stats = {i['Series']:i['Value']}
# print(stats)
# print(value)
if (i['ID']== '4'):
cuntry = True
if cuntry == True:
if location not in country_data:
country_data[location] = {}
if years not in country_data[location]:
country_data[location][years] = {}
if series not in country_data[location][years]:
country_data[location][years][series] = value
else:
if location not in region_data:
region_data[location] = {}
if years not in region_data[location]:
region_data[location][years] = {}
if series not in region_data[location][years]:
region_data[location][years][series] = value
为了澄清,这里显示的是一个“地区”作为字典中的关键字。年份是该地区字典中的值和关键字,以此类推。。。。你知道吗
我想了解如何循环浏览数据并回答以下问题:
从2005年到2015年,哪个地区的孕产妇死亡率下降幅度最大?你知道吗
“孕产妇死亡率(每10万人口死亡数)”是词典中的一个关键。你知道吗
使用熊猫,并根据这个answer读取你的文件。你知道吗
然后可以为“Region/Country/Area”和“Series”创建一个轴,并将其用作聚合函数“max”。你知道吗
然后按序列名对“透视表”排序,并使用参数“升序”
最后你会得到你问题的答案。你知道吗
如果希望在Python3.x中循环遍历字典,可以使用每个字典中的方法.items(),并用三个循环嵌套它们。你知道吗
通过一个名为hear dict\u total的主字典,这段代码可以实现它。你知道吗
相关问题 更多 >
编程相关推荐