简体   繁体   English

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

[英]how to format list of dictionaries to nested dictionary in python

I am fetching huge data as list of dictionary.我正在获取大量数据作为字典列表。 But, I would like to reformat as nested dictionaries.但是,我想重新格式化为嵌套字典。 I am not sure how nested dictionary work.我不确定嵌套字典是如何工作的。 I would like add the code I have tried but seems I am getting error while posting.我想添加我尝试过的代码,但似乎在发布时出错。 Can't able to add more codes.无法添加更多代码。 I will add in comment section.我会在评论区补充。 My list of dictionaries looks in this.我的字典列表在此。

    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"
  }

I would like to format like this.我想这样格式化。

    {
    "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"
        }
    }
}

The code I have tried.我试过的代码。

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

print(list_dict)

I would like to write the output to csv file.我想将输出写入 csv 文件。 I would appreciate any help.我将不胜感激任何帮助。

try this, usingsetdefault to init missing keys & update the values on go.试试这个,使用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'}}}

You could do something like:你可以这样做:

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM