比较字典列表中特定值的两个字典

时间:2019-02-17 04:01:23

标签: python python-3.x

我有两个字典清单。我正在遍历它们并寻找匹配的ID。如果src_dict中的id与目标匹配,我需要调用一个update方法,否则调用一个insert方法。当我使用下面的代码时,我得到了意外的结果。

这是我想要的结果。更新时,我需要将dest dict中的rec_id与src dict中的相应值保留在一起。插入几乎只是src dict元素,而dest dict中却没有。感谢任何帮助!

records_update = [{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}}, {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'name_changed_to_not_ross'}}]
#. the rec_id is from dest_dict while rest of the field should come from src_dict since these values could change that needs to be update
records_insert = [{"id": 444, "name": "jack"}]

src_dict = [{"id": 111, "name": "sam"}, {"id": 333, "name": "name_changed_to_not_ross"}, {"id": 444, "name": "jack"}]

dest_dict = [{"rec_id":"abc","fields":{"id":111,"name":"sam"}}, 
                {"rec_id":"pqr","fields":{"id":222,"name":"john"}},
                 {"rec_id":"xyz","fields":{"id":333,"name":"ross"}}]


records_update = []
records_insert = []
for rec_src in src_dict:
        for rec_dest in dest_dict:
            if rec_src['id'] == rec_dest['fields']['id']:
                print('match and add this element to update list')
                records_update.append(rec_src)
            else:
                print('no match add this element to insert list')
                records_insert.append(rec_src)```

3 个答案:

答案 0 :(得分:2)

您可以创建一个由dest_dict的ID索引的字典,以进行有效的查找,然后使用列表推导过滤src_dict的各个记录:

dest = {d['fields']['id']: d for d in dest_dict}
records_update = [dest[d['id']] for d in src_dict if d['id'] in dest]
records_insert = [d for d in src_dict if d['id'] not in dest]

答案 1 :(得分:0)

仅当不执行任何追加操作来更新列表时,才需要执行插入操作以插入列表。

records_update = []
records_insert = []
for rec_src in src_dict:
    flag = 0
    for rec_dest in dest_dict:
        if rec_src['id'] == rec_dest['fields']['id']:
            print('match and add this element to update list')
            records_update.append(rec_dest)
            flag = 1
    if flag != 1:
        print('no match add this element to insert list')
        records_insert.append(rec_src)

print(records_update)
# [{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}}, {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'ross'}}]
print(records_insert)
# [{'id': 444, 'name': 'jack'}]

答案 2 :(得分:0)

我可以使用以下方法达到预期的效果:

records_update = []
records_insert = []
for rec_src in src_dict:
    flag = 0
    for rec_dest in dest_dict:
        if rec_src['id'] == rec_dest['fields']['id']:
            new_dict = {}
            print('match and add this element to update list')
            new_dict['rec_id'] = rec_dest['rec_id']
            new_dict['fields'] = rec_src
            records_update.append(new_dict)
            flag = 1
    if flag != 1:
        print('no match add this element to insert list')
        records_insert.append(rec_src)

records_update
[{'rec_id': 'abc', 'fields': {'id': 111, 'name': 'sam'}},
 {'rec_id': 'xyz', 'fields': {'id': 333, 'name': 'name_changed_to_not_ross'}}]
records_insert
[{'id': 444, 'name': 'jack'}]

只是好奇我如何使用@blhsing方法获得结果,因为这似乎是更有效的方法

ids = {d['fields']['id'] for d in dest_dict}
records_update = [dest[d['id']] for d in src_dict if d['id'] in dest]
records_insert = [d for d in src_dict if d['id'] not in dest]