如何在python中将字典列表格式化为嵌套字典

时间:2020-08-28 08:43:13

标签: python json python-3.x dictionary

我正在获取大量数据作为字典列表。但是,我想重新格式化为嵌套字典。我不确定嵌套字典的工作方式。我想添加我尝试过的代码,但发布时似乎出现错误。无法添加更多代码。我将在评论部分中添加。 我的词典列表在其中。

    source = [
  {
    "account_id": "111111111111",
    "instance_id": "i-xxxxxxxxxxx",
    "instance_profile_arn": "arn:aws:iam::111111111111:instance-profile/x",
    "region_name": "eu-central-1"
  },
  {
    "account_id": "111111111111",
    "instance_id": "i-aaaaaaaaaaa",
    "instance_profile_arn": "arn:aws:iam::111111111111:instance-profile/aa",
    "region_name": "us-east-1"
  },
  {
    "account_id": "22222222222",
    "instance_id": "i-bbbbbbbbb",
    "instance_profile_arn": "arn:aws:iam::22222222222:instance-profile/sadf",
    "region_name": "eu-central-1"
  },
  {
    "account_id": "22222222222",
    "instance_id": "i-ccccccccccc",
    "instance_profile_arn": "arn:aws:iam::22222222222:instance-profile/sds",
    "region_name": "us-east-1"
  },
  {
    "account_id": "33333333333",
    "instance_id": "i-eeeeeeeee",
    "instance_profile_arn": "arn:aws:iam::33333333333:instance-profile/dsf",
    "region_name": "eu-west-1"
  }

我要这样格式化。

    {
    "111111111111": {
        "eu-central-1": {
            "i-xxxxxxxxxxx": "arn:aws:iam::111111111111:instance-profile/x"
        },
        "us-east-1": {
            "i-aaaaaaaaaaa": "arn:aws:iam::111111111111:instance-profile/aa"
        }
    },
    "22222222222": {
        "eu-central-1": {
            "i-bbbbbbbbb": "arn:aws:iam::22222222222:instance-profile/sds"
        },
        "us-east-1": {
            "i-ccccccccccc": "arn:aws:iam::22222222222:instance-profile/sds"
        }
    },
    "33333333333": {
        "eu-west-1": {
            "i-eeeeeeeee": "arn:aws:iam::33333333333:instance-profile/dsf"
        }
    }
}

我尝试过的代码。

for each in source:
    list_dict.append({
        each['account_id']: {
            each['region_name']: {
                each['instance_id']: each['instance_profile_arn']
            }
        }
    })

print(list_dict)

我想将输出写入csv文件。 我将不胜感激。

2 个答案:

答案 0 :(得分:2)

尝试使用setdefault初始化丢失的键并随时更新值。

source = { .. } # source dict

result = {}

for v in source:
    (result.setdefault(v['account_id'], {})
        .setdefault(v['region_name'], {})
        .update({v['instance_id']: v['instance_profile_arn']}))

print(result)

{'111111111111': {'eu-central-1': {'i-xxxxxxxxxxx': 'arn:aws:iam::111111111111:instance-profile/x'},
                  'us-east-1': {'i-aaaaaaaaaaa': 'arn:aws:iam::111111111111:instance-profile/aa'}},
 '22222222222': {'eu-central-1': {'i-bbbbbbbbb': 'arn:aws:iam::22222222222:instance-profile/sadf'},
                 'us-east-1': {'i-ccccccccccc': 'arn:aws:iam::22222222222:instance-profile/sds'}},
 '33333333333': {'eu-west-1': {'i-eeeeeeeee': 'arn:aws:iam::33333333333:instance-profile/dsf'}}}

答案 1 :(得分:0)

您可以执行以下操作:

 result = dict();
 for item in source:
     account: = item['account_id']
     region = item['region_name']
     
     if account not in result:
         result[account] = dict()
     
     if region not in result[account]:
         result[account][region] = dict()
     
     instance = item['instance_id']
     instance_profile = item['instance_profile_arn']
     result[account][region][instance] = instance_profile