栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

两个python字典(键和值)的递归差异

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

两个python字典(键和值)的递归差异

一种选择是将您遇到的所有列表转换成字典,并将索引作为键。例如:

# add this function to the same moduledef list_to_dict(l):    return dict(zip(map(str, range(len(l))), l))# add this pre under the 'if type(d2[k]) == dict' block         elif type(d2[k]) == list:  dd(list_to_dict(d1[k]), list_to_dict(d2[k]), k)

这是带有注释的示例词典的输出:

>>> d1 = {"name":"Joe", "Pets":[{"name":"spot", "species":"dog"}]}>>> d2 = {"name":"Joe", "Pets":[{"name":"spot", "species":"cat"}]}>>> dd(d1, d2, "base")Changes in baseChanges in PetsChanges in 0species changed in d2 to catDone with changes in 0Done with changes in PetsDone with changes in base

请注意,这将按索引对索引进行比较,因此需要进行一些修改才能很好地适用于添加或删除的列表项。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/441119.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号