如何将字典值映射到另一个字典

2024-10-01 22:28:45 发布

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

我有一本字典在下面

{
    "aggregations": { 
        "A": { 
            "doc_count_error_upper_bound": 0, 
            "sum_other_doc_count": 0, 
            "buckets": [ 
                { "key": "ADL", "doc_count": 1 },
                { "key": "SDD", "doc_count": 1 }, 
                { "key": "JJD", "doc_count": 1 }
            ] 
        }, 
        "B": { 
            "doc_count_error_upper_bound": 0, 
            "sum_other_doc_count": 0, 
            "buckets": [ 
                { "key": "ABC", "doc_count": 1 }, 
                { "key": "CDE", "doc_count": 1 }, 
                { "key": "FGH", "doc_count": 1 } 
            ] 
        }, 
        "C": { 
            "doc_count_error_upper_bound": 0, 
            "sum_other_doc_count": 0, 
            "buckets": [ 
                { "key": "XYX", "doc_count": 1 }, 
                { "key": "NXS", "doc_count": 1 } 
            ] 
         } 
    }
} 
  • aggregations.keys将是aggregationfilters.fieldName

  • aggregations.bucket.key将是aggregationfilters.values.title

  • aggregationfilters.values.paragration每次都为null

  • aggregations.bucket.doc\u计数将是aggregationfilters.values.count

  • 基本上,我需要提取aggregations.keys和aggregations.bucket值并放入不同的字典中

需要编写一个通用的代码结构来实现这一点

我不能用.弹出(重命名)字典

我的预期出局了

{
    "aggregationfilters": [ 
        { 
            "name": "ABC", 
            "fieldName": "A", 
            "values": [ 
                { "title": "ADL", "paragraph": null, "count": 1 }, 
                { "title": "SDD", "paragraph": null, "count": 1 }, 
                { "title": "JJD", "paragraph": null, "count": 1 }
            ] 
        }, { 
            "name": "CDE", 
            "fieldName": "B", 
            "values": [ 
                { "title": "ABC", "paragraph": null, "count": 1 }, 
                { "title": "CDE", "paragraph": null, "count": 1 }, 
                { "title": "FGH", "paragraph": null, "count": 1 } 
            ] 
        }, { 
            "name": "FGH", 
            "fieldName": "C", 
            "values": [ 
                { "title": "XYX", "paragraph": null, "count": 1 }, 
                { "title": "NXS", "paragraph": null, "count": 1 }
            ] 
        }
    ]
}

Tags: keydoc字典titlecounterroruppernull
1条回答
网友
1楼 · 发布于 2024-10-01 22:28:45

好吧,这是可行的,但即使我尽了最大努力,这看起来还是不干净

import json

source = {
    "aggregations": {
        "A": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {"key": "ADL", "doc_count": 1},
                {"key": "SDD", "doc_count": 1},
                {"key": "JJD", "doc_count": 1},
            ],
        },
        "B": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {"key": "ABC", "doc_count": 1},
                {"key": "CDE", "doc_count": 1},
                {"key": "FGH", "doc_count": 1},
            ],
        },
        "C": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [{"key": "XYX", "doc_count": 1}, {"key": "NXS", "doc_count": 1}],
        },
    }
}


convert_map = {
    "buckets": "values",
    "doc_count": "count",
    "key": "title",
}

remove_map = {"sum_other_doc_count", "doc_count_error_upper_bound"}

add_map = {"name": "Changed VAL_", "fieldName": "VAL_"}


def converting_generator(
    source_: dict, convert_map_: dict, remove_map_: set, add_map_: dict
):
    working_dict = {k: v for k, v in source_.items()}
    variable_identifier = "VAL_"

    for key, inner_dic in working_dict.items():
        inner_dic: dict
        for rm_key in remove_map_:
            try:
                inner_dic.pop(rm_key)
            except KeyError:
                pass

        for add_key, add_val in add_map_.items():
            inner_dic[add_key] = add_val.replace(variable_identifier, key)

        dumped = json.dumps(inner_dic, indent=2)

        for original, target in convert_map_.items():
            dumped = dumped.replace(original, target)

        yield json.loads(dumped)


converted = {
    "aggregation_filters": list(
        converting_generator(source["aggregations"], convert_map, remove_map, add_map)
    )
}

for inner_dict in converted["aggregation_filters"]:
    for even_inner_dict in inner_dict["values"]:
        even_inner_dict["paragraph"] = None

print(json.dumps(converted, indent=2))

输出:

{
  "aggregation_filters": [
    {
      "values": [
        {
          "title": "ADL",
          "count": 1,
          "paragraph": null
        },
        {
          "title": "SDD",
          "count": 1,
          "paragraph": null
        },
        {
          "title": "JJD",
          "count": 1,
          "paragraph": null
        }
      ],
      "name": "Changed A",
      "fieldName": "A"
    },
    {
      "values": [
        {
          "title": "ABC",
          "count": 1,
          "paragraph": null
        },
        {
          "title": "CDE",
          "count": 1,
          "paragraph": null
        },
        {
          "title": "FGH",
          "count": 1,
          "paragraph": null
        }
      ],
      "name": "Changed B",
      "fieldName": "B"
    },
    {
      "values": [
        {
          "title": "XYX",
          "count": 1,
          "paragraph": null
        },
        {
          "title": "NXS",
          "count": 1,
          "paragraph": null
        }
      ],
      "name": "Changed C",
      "fieldName": "C"
    }
  ]
}

始终显示您的代码,若这是一个有效的代码,那个就太好了——表明您已经在您的问题上付出了至少那个么值得的努力

我不觉得麻烦,因为这感觉像是解谜,但其他人可能不会

相关问题 更多 >

    热门问题