如何比较两个字典并更新

时间:2020-11-06 10:33:27

标签: python dictionary

嗨,我有两个字典1.Primary,2.Secondary

  • 需要检查两个字典的前field

  • 如果提交的内容相同,则将标题与主要和次要进行比较

  • 如果Primary字典中缺少字典,请将其添加到Primary

主要字典

{"Latest":[
  {
    "name": "Employee",
    "field": "employee",
    "values": [
      {
        "title": "A",
        "paragraph": "null",
        "role": "Deveoper"
      },
      {
        "title": "C",
        "paragraph": "null",
        "role": "Tester"
      }
    ]
  },
  {
    "name": "Project",
    "field": "project",
    "values": [
      {
        "title": "NEW_York",
        "paragraph": "null",
        "role": "Long Term"
      }
    ]
  },
  {
    "name": "Designation",
    "field": "designation",
    "values": [
      {
        "title": "Developer",
        "paragraph": "null",
        "role": "null"
      }
    ]
}
]}

中学词典

[
  {
    "name": "Employee",
    "field": "employee",
    "values": [
      {
        "title": "A",
        "paragraph": "null",
        "role": "null"
      },
      {
        "title": "B",
        "paragraph": "null",
        "role": "null"
      }
    ]
  },
  {
    "name": "Project",
    "field": "project",
    "values": [
      {
        "title": "NEW_York",
        "paragraph": "test",
        "role": "null"
      }
    ]
  },
  {
    "name": "Designation",
    "field": "designation",
    "values": [
      {
        "title": "Tester",
        "paragraph": "null",
        "role": "null"
      }
    ]
}
]

预期

{"Latest":[
  {
    "name": "Employee",
    "field": "employee",
    "values": [
      {
        "title": "A",
        "paragraph": "null",
        "role": "Deveoper"
      },
      {
        "title": "C",
        "paragraph": "null",
        "role": "Tester"
      },
      {
        "title": "B",
        "paragraph": "null",
        "role": "null"
      }
    ]
  },
  {
    "name": "Project",
    "field": "project",
    "values": [
      {
        "title": "NEW_York",
        "paragraph": "null",
        "role": "Long Term"
      }
    ]
  },
  {
    "name": "Designation",
    "field": "designation",
    "values": [
      {
        "title": "Developer",
        "paragraph": "null",
        "role": "null"
      },
      {
        "title": "Tester",
        "paragraph": "null"
        "role": "null"
      }
    ]
}
]}

代码

 for i in primary['Latest']:
    for j in secondary:
        if i['field'] == j['field']:
            for a in i['values']:
                for b in j['values']:
                    if a['title'] == b['title']:
                        i['values'].append(b)

我的代码无限期执行

1 个答案:

答案 0 :(得分:2)

问题是您要在i['values']上进行迭代时追加,从而产生意外的行为。如果i['values']像这样,可以通过迭代“冻结”列表来解决此问题:

from copy import deepcopy

for i in primary['Latest']:
    for j in secondary:
        if i['field'] == j['field']:
            values = deepcopy(i['values'])
            for a in values:
                for b in j['values']:
                    if a['title'] == b['title']:
                        i['values'].append(b)

这解决了“无限期”的部分,但效果不佳,这是输出:

print(json.dumps(primary, indent=2))
{
  "Latest": [
    {
      "name": "Employee",
      "field": "employee",
      "values": [
        {
          "title": "A",
          "paragraph": null,
          "role": "Deveoper"
        },
        {
          "title": "C",
          "paragraph": null,
          "role": "Tester"
        },
        {
          "title": "A",
          "paragraph": null,
          "role": "null"
        }
      ]
    },
    {
      "name": "Project",
      "field": "project",
      "values": [
        {
          "title": "NEW_York",
          "paragraph": "null",
          "role": "Long Term"
        },
        {
          "title": "NEW_York",
          "paragraph": "test",
          "role": "null"
        }
      ]
    },
    {
      "name": "Designation",
      "field": "designation",
      "values": [
        {
          "title": "Developer",
          "paragraph": null,
          "role": "null"
        }
      ]
    }
  ]
}

所以我修复了一点:


for primary_elem in primary['Latest']:
    primary_titles = [value['title'] for value in primary_elem['values']]
    for secondary_elem in secondary:
        if secondary_elem['field'] == primary_elem['field']:
            for secondary_value in secondary_elem['values']:
                if secondary_value['title'] not in primary_titles:
                    primary_elem['values'].append(secondary_value)

哪个给

>>> print(json.dumps(primary, indent=2))
{
  "Latest": [
    {
      "name": "Employee",
      "field": "employee",
      "values": [
        {
          "title": "A",
          "paragraph": null,
          "role": "Deveoper"
        },
        {
          "title": "C",
          "paragraph": null,
          "role": "Tester"
        },
        {
          "title": "B",
          "paragraph": null,
          "role": "null"
        }
      ]
    },
    {
      "name": "Project",
      "field": "project",
      "values": [
        {
          "title": "NEW_York",
          "paragraph": "null",
          "role": "Long Term"
        }
      ]
    },
    {
      "name": "Designation",
      "field": "designation",
      "values": [
        {
          "title": "Developer",
          "paragraph": null,
          "role": "null"
        },
        {
          "title": "Tester",
          "paragraph": null,
          "role": "null"
        }
      ]
    }
  ]
}