首页 > 解决方案 > 将一个嵌套字典与嵌套字典列表进行比较

问题描述

我目前正在尝试根据嵌套字典是否出现在该字典中,从嵌套字典列表的字典中删除嵌套字典,并返回过滤后的字典。

所以,

large_dict = {

k1 : [{id: 123, name: 'foo', type: 'thing', places: ['hobbiton', 'narnia']}, 
{id: 456, name: 'bar', type: 'gizmo', places: NaN}, 
{id: 789, name: 'python', type: 'thingamajig', places: NaN}],

k2: [{id: 101, name: 'app', type: 'bauble'}]

}

valid_dict = {

k1: {id: 123, name: 'foo', type: 'thing', places: ['hobbiton', 'narnia']}, 
k2: {id: 101, name: 'app', type: 'bauble', places: NaN}

}

所需的输出是返回large_dict但没有valid_dict,因此所需的输出是返回所有未出现在的字典valid_dict,另一个障碍是某些值只出现在大字典中一次,所以我不想摆脱它们:

filtered_dict = {

    k1 : [{id: 456, name: 'bar', type: 'gizmo', places: NaN}, 
    {id: 789, name: 'python', type: 'thingamajig', places: NaN}],

    k2: {id: 101, name: 'app', type: 'bauble', places: NaN}

    }

我目前已经尝试过反向交叉,但我不能完全解决它。还有这段代码,但我不断得到

un_rel_dict[a] = [{t['id']: t['attributes.name']}, {'type':t['type']}]
TypeError: string indices must be integers

unmatched_dict = {}
for (k, v), (i, j) in zip(best_match.items(), match_dict.items()):
    unmatched_dict[k] = [x for x in v if not x in j]

任何帮助是极大的赞赏!

标签: pythonpython-3.x

解决方案


推荐阅读