循环遍历Python中的嵌套字典

2024-10-02 10:22:59 发布

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

所以我需要帮助循环通过一个嵌套的字典,我已经创建,以回答一些问题。我的代码将两个不同的字典拆分并向其中添加项,如下所示: 链接到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

当我打印字典区域时,数据输出是: enter image description here

为了澄清,这里显示的是一个“地区”作为字典中的关键字。年份是该地区字典中的值和关键字,以此类推。。。。你知道吗

我想了解如何循环浏览数据并回答以下问题:

从2005年到2015年,哪个地区的孕产妇死亡率下降幅度最大?你知道吗

“孕产妇死亡率(每10万人口死亡数)”是词典中的一个关键。你知道吗


Tags: csvtheindataif字典valuenot
2条回答
  1. 构建数据帧

使用熊猫,并根据这个answer读取你的文件。你知道吗

import pandas as pd
filename = 'dph_SYB60_T03_Population Growth, Fertility and Mortality Indicators.csv'
df = pd.read_csv(filename)
  1. 构建数据透视表

然后可以为“Region/Country/Area”和“Series”创建一个轴,并将其用作聚合函数“max”。你知道吗

pivot = df.pivot_table(index='Region/Country/Area', columns='Series', values='Value', aggfunc='max')
  1. 按您感兴趣的系列排序

然后按序列名对“透视表”排序,并使用参数“升序”

df_sort = pivot.sort_values(by='Maternal mortality ratio (deaths per 100,000 population)', ascending=False)
  1. 提取第一行中的最大值。你知道吗

最后你会得到你问题的答案。你知道吗

df_sort['Maternal mortality ratio (deaths per 100,000 population)'].head(1)


Region/Country/Area
Sierra Leone    1986.0
Name: Maternal mortality ratio (deaths per 100,000 population), dtype: float64

Warning: Some of your regions have records before 2005, so you should filter your data only for values between 2005 and 2015.

如果希望在Python3.x中循环遍历字典,可以使用每个字典中的方法.items(),并用三个循环嵌套它们。你知道吗

通过一个名为hear dict\u total的主字典,这段代码可以实现它。你知道吗

out_region = None
out_value = None
sel_serie = 'Maternal mortality ratio (deaths per 100,000 population)'
min_year = 2005
max_year = 2015

for reg, dict_reg  in dict_total.items():
    print(reg)
    for year, dict_year in dict_reg.items():
        if min_year <= year <= max_year:
            print(year)
            for serie, value in dict_year.items():
                if serie == sel_serie and value is not None:
                    print('{} {}'.format(serie, value))
                    if out_value is None or out_value < value:
                        out_value = value
                        out_region = reg

 print('Region: {}\nSerie: {} Value: {}'.format(out_region, sel_serie, out_value))

相关问题 更多 >

    热门问题