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

非斯

我有下面的字典

{
    "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.buckets.key将是aggregationfilters.values.title

  • 每次每次aggregationfilters.values.paragraph为null

  • gregations.buckets.doc_count将是aggregationfilters.values.count

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

需要编写一个通用的代码结构来做到这一点。

我无法使用.pop(rename)dictioanry

我的预期了

{
    "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 }
            ] 
        }
    ]
}
朱庇特比

好吧,这行得通,但是即使尽我最大的努力,这看起来也不是那么干净。

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

始终显示您的代码,如果这是可行的话,那会很好-显示您至少在问题上付出了宝贵的努力。

我不会打扰它,因为这就像解决难题一样,但是其他人可能不会。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

将字典映射到另一个字典

如何将字典的值设置为对另一个字典的值的引用?

如何将字典的键值与另一个字典的值匹配并附加结果?

有一种简单的方法可以将一个字典映射到另一个字典吗?

如何将值从一个字典更新到另一个

如何使用C#使用字典将位于一个字符串列表中的键映射到位于另一个字符串列表中的值?

如何将字典的内容复制到另一个字典以在之后清除原始字典

将字典的值列表替换为另一个字典中的对应键值映射

将嵌套字典值与另一个字典值相乘

如何将项的字典添加到另一个字典中

有没有一种 Pythonic 方式将字典映射到另一个字典?

如何根据另一个字典的值列表拆分字典的值列表?

如何根据另一个字典的值列表拆分字典的值列表?

如何使用python中另一个字典的值过滤字典?

如何修改另一个字典中的字典值

将一个字典关键字与另一个字典值进行比较,然后创建一个新的字典

如何将一个字符数组映射到另一个字符数组?

如何从另一个字典中减去一个字典的值并将结果写入第三个字典?

如何映射键,在另一个字典中查找键?

如何将另一个字典条目添加到现有字典以形成新字典(即不追加)

如何根据一个字典在 Python 中查询另一个字典的值来修改或创建字典

映射来自另一个字典的字典列表的元素

用一个计数将一个字典的值转换为另一个字典

比较另一个字典中两个字典的值

如何将字典中的相同值分配给另一个字符串列表

将字典中的值列表除以另一个字典中的值列表

将键与字典中的值匹配,并使用 ansible 替换匹配另一个字典的值

将一个字典中的值与 Python 中另一个字典中的值进行比较