首页 > 解决方案 > Python:如何在列表列表中添加新字典?

问题描述

如何将新的 {key:value} 添加到列表中的列表?我必须创建一个更新过滤器的方法。有一些默认过滤器,我必须添加新的或更新现有的。

def _prepare_filters_json(self, pipeline):
    filters = self._get_filters(pipeline) 

这将返回一个默认过滤器配置,我必须在其中添加新过滤器或更改现有过滤器。

        "groupCondition": "OR",
        "children": [
          {
            "operatorId": "equal to",
            "field": {
              "id": "0a39a3170a9e10000572a0456cb201c4",
              "descriptor": "Custom Active Status",
              "returnType": "Boolean"
            },
            "targetBoolean": true,
            "id": "152544fb-1158-438e-86f5-62a45ba50193",
            "type": "CONDITION"
          },
          {
            "groupCondition": "AND",
            "children": [
              {
                "operatorId": "greater than or equal to",
                "field": {
                  "id": "d1e5e42098374581ab37a3f45e63430b",
                  "descriptor": "Termination Date",
                  "returnType": "Date"
                },
                "targetDate": "2021-07-20",
                "id": "dab1e00f-e902-40f2-bfb2-9556164d2f69",
                "type": "CONDITION"
              },
              {
                "operatorId": "is",
                "field": {
                  "id": "d1e5e42098374581ab37a3f45e63430b",
                  "descriptor": "Termination Date",
                  "returnType": "Date"
                },
                "targetDate": "2021-07-21",
                "id": "b9393930-d2aa-4631-a037-7147b818033e",
                "type": "CONDITION"
              }
            ],
            "id": "93de0226-9e4c-498b-9fcf-6c86dd3e77bc",
            "type": "GROUP"
          },
          {
            "operatorId": "is empty",
            "field": {
              "id": "d02efbd173e745beba3217b4293bafe2",
              "descriptor": "Employee",
              "returnType": "Single instance"
            },
            "id": "d3838c04-33ea-4eb7-b8b8-be471c09ec9c",
            "type": "CONDITION"
          },
          {
            "operatorId": "not in the selection list",
            "field": {
              "id": "4f340a62a0634f54839981ffeb304918",
              "descriptor": "Country of Birth (Locale Sensitive)",
              "returnType": "Single instance"
            },
            "targetInstances": [
              {
                "id": "54c5b6971ffb4bf0b116fe7651ec789a",
                "descriptor": "France"
              }
            ],
            "id": "9f37da93-6023-4390-aedb-b309bf6adb5c",
            "type": "CONDITION"
          }
        ],
        "id": "c8f8cc91-51ce-43ec-ae33-e4295c367aa9",
        "type": "GROUP"
      }

要添加一个新字典,我必须创建它,但在这种情况下,会覆盖我拥有的内容并使用单个“”返回所有内容。

        filters = self._get_filters(pipeline)
        source_fields = self.ssi_get_filter_source_fields(pipeline)
        for source_field in source_fields.json_path("$.data"):
                if source_field["descriptor"] == filter_source_field:
                    filters["children"] = {}
                    filters["children"]["type"] = 'CONDITION'
                    filters["children"]["id"] = 123
                    filters["children"]["field"] = source_field
                    filters["children"]["operatorId"] = cond
                    filters["children"]["targetText"] = test

        return self._prepare_json(pipelineId=pipeline, filter=filters)

响应(configid 和其余部分由 _prepare_json 方法添加)

{'configurationId': '16918ba62e5a01c119d648282600cbc1',
 'filter': {'children': {'field': {'descriptor': 'Job Profile',
                                   'id': 'aa15f19ba55842cf8deb1b63fc129fc9',
                                   'returnType': 'Single instance'},
                         'id': 123,
                         'operatorId': 'equal to',
                         'targetText': 'test',
                         'type': 'CONDITION'},
            'groupCondition': 'OR',
            'id': '61b0a93e-d8a9-4e27-82fc-f1e9bd3122c5',
            'type': 'GROUP'},
 'pipelineId': 'worker',
 'version': 1}

如何将我的 dict 添加到现有列表中?为什么它有引号?

标签: pythonpython-3.xlistdictionary

解决方案


推荐阅读