python - 将一个嵌套字典与嵌套字典列表进行比较
问题描述
我目前正在尝试根据嵌套字典是否出现在该字典中,从嵌套字典列表的字典中删除嵌套字典,并返回过滤后的字典。
所以,
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]
任何帮助是极大的赞赏!
解决方案
推荐阅读
- javascript - 即使在页面重新加载后如何在浏览器扩展中连续运行 JavaScript 代码
- c# - 使用 C# API 进行转换时忽略 XSLT 中的 FileNotFound
- python - 如何在 PySimpleGUI 的窗口中显示图像?
- amazon-web-services - 如何使用 AWS 上的密钥对访问 SFTP 服务器上的客户端以进行 s3 存储桶访问?
- docker - 是否有可能知道 docker-compose 运行使用了多少内存?
- javascript - 如何获取等于或大于 X 但小于数组中下一个 X 的元素?
- python - 用 BeautifulSoup 抓取后提取单词
- javascript - 在 keydown 上替换文本字段中的字符
- python - 根据 os.system() 的输出使构建/脚本失败
- javascript - 如何在 Ant Design Tab 组件中使用 React Router Link 组件