如何将所有键从嵌套dict插入到新的嵌套dict?

2024-10-01 17:41:27 发布

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

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

具有嵌套字典的变量:

{'~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'}

Tags: textinselfidfieldsourcefields字典
3条回答

我认为在Python的字典中不能有与您提到的键相同的重复项

我想你应该有一个包含所有单条命令的列表,而不是嵌套在一个命令中

你可以做一些棘手的事情,比如here

所以,我发现了一个错误,现在它可以正常工作了

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)

我不完全确定我是否理解您正在尝试做的事情,但是我认为您需要的是一个字段列表,该列表被移动到新的\u过滤器字典中。在这种情况下,我将在新的过滤器dict中创建一个字段列表,并将对象附加到此列表中,因此您的输出将如下所示:

{'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)

相关问题 更多 >

    热门问题