【发布时间】: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