【问题标题】:How to insert all keys from nested dict to new nested dict?如何将嵌套字典中的所有键插入新的嵌套字典?
【发布时间】:2021-07-31 21:07:27
【问题描述】:

我正在编写测试自动化,并且必须将变量中的所有值插入到新字典中,但由于某种原因,它总是只取最后一个。可能是什么原因?

带有嵌套字典的变量:

{'~Manager~': {"operatorId": 'in the selection list'},
 'Candidate': {"AND":"", "operatorId": 'not in the selection list'},
  ...
 'Description': {"operatorId": 'is empty'}}
def _prepare_filters_json(self, pipeline: str, filter_to_add: dict):
    new_filter = {"type": 'CONDITION', "id": 'any_id', "field": 'null',
                  "operatorId": 'null'}
    source_fields = self.ssi_get_filter_source_fields(pipeline).json_path("$.data")
    filters = self._get_filters(pipeline)
    for source_field in source_fields:
        for key in filter_to_add.keys():
            if key == source_field["descriptor"]:
                new_filter["field"] = source_field
    return self._prepare_json(pipelineId=pipeline, filter=filters)

source_fields 返回字典列表。如果该列表中的值是 == 到我变量中的 dict.keys 它应该更新我的 new_filter

实际结果:

{'field': {'descriptor': 'Description',
           'id': 'edee9a85b3fb4cb69b993139fc14ce46',
           'returnType': 'Text'},
 'id': 'any_id',
 'operatorId': 'null',
 'type': 'CONDITION'}

预期结果:

{'field': {'descriptor': '~Manager~',
           'id': 'edee9a85b3fb4cb69b993139fc123451',
           'returnType': 'Text'},
'field': {'descriptor': 'Candidate',
           'id': 'edee9a85b3fb4cb69b993139fc141111',
           'returnType': 'Text'},
'field': {'descriptor': 'Description',
           'id': 'edee9a85b3fb4cb69b993139fc14ce46',
           'returnType': 'Text'},
 'id': 'any_id',
 'operatorId': 'null',
 'type': 'CONDITION'}

【问题讨论】:

  • "预期结果:" any 代码无法创建此结果。 dict 的键是唯一的。这就是为什么键查找可以确定性地工作的原因。

标签: python dictionary nested


【解决方案1】:

我不完全确定我理解你想要做什么,但是我认为你想要的是一个移动到你的 new_filter 字典中的字段列表。在这种情况下,我会在你的 new_filter 字典中创建一个字段列表并将对象附加到这个列表中,所以你的输出看起来像这样:

{'fields': [{'descriptor': '~Manager~',
           'id': 'edee9a85b3fb4cb69b993139fc123451',
           'returnType': 'Text'},
{'descriptor': 'Candidate',
           'id': 'edee9a85b3fb4cb69b993139fc141111',
           'returnType': 'Text'},
{'descriptor': 'Description',
           'id': 'edee9a85b3fb4cb69b993139fc14ce46',
           'returnType': 'Text'}],
 'id': 'any_id',
 'operatorId': 'null',
 'type': 'CONDITION'}

这可以通过执行以下操作来实现:

def _prepare_filters_json(self, pipeline: str, filter_to_add: dict):
  new_filter = {"type": 'CONDITION', "id": 'any_id', "fields": [], "operatorId": 'null'}
  source_fields = self.ssi_get_filter_source_fields(pipeline).json_path("$.data")
  filters = self._get_filters(pipeline)
  for source_field in source_fields:
    for key in filter_to_add.keys():
      if key == source_field["descriptor"]:
        new_filter["fields"].append(source_field)
  return self._prepare_json(pipelineId=pipeline, filter=filters)

【讨论】:

  • 这不是我想要的。我正在测试 BE,身体应该看起来像预期的结果。而且你不能追加到一个字符串。
  • 预期结果是不可能的,因为您在单个字典中有 3 个称为字段的键。字典中的键必须是唯一的。我在建议的代码中将字段转换为列表(不是字符串),以规避使用 3 个具有相同名称的键的问题,而是使用可以将对象附加到的列表。但是,我似乎不明白您要达到的目标。 :)
【解决方案2】:

我不认为您可以使用与您在 Python 中的字典中提到的相同键的重复项。

我认为您宁愿拥有一个包含所有单独字典的列表,而不是嵌套在字典中。

你可以做一些像here描述的棘手的事情。

【讨论】:

  • 但我不想重复。我想获取第一个键:值 - {'~Manager~': {"operatorId": 'in the selection list'}} 并执行我的代码,将其添加到 new_dictionary 中的字段。然后取第二个 key:value {'Candidate':{"AND":"", "operatorId": 'not in the selection list'} 并通过向 new_dict 添加另一个字段再次执行我的代码.
【解决方案3】:

所以,我发现了一个错误,现在它按预期工作了。

def _prepare_filters_json(self, pipeline: str, filter_to_add: dict):
    new_filter = {"type": 'CONDITION', "id": 'any_id', "field": 'null',
                  "operatorId": 'null'}
    source_fields = self.ssi_get_filter_source_fields(pipeline).json_path("$.data")
    filters = self._get_filters(pipeline)
    for key in filter_to_add.keys():
        for source_field in source_fields:
          if source_field["descriptor"] == key:
            new_filter["field"] = source_field
            filters["children"].append(dict(new_filter))
    return self._prepare_json(pipelineId=pipeline, filter=filters)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    • 1970-01-01
    • 2018-01-29
    • 2014-08-24
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多