首页 > 解决方案 > 如何比较两个字典并更新

问题描述

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

初级词典

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

我的代码无限期执行

标签: pythondictionary

解决方案


问题是您在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"
        }
      ]
    }
  ]
}

推荐阅读