一种选择是将您遇到的所有列表转换成字典,并将索引作为键。例如:
# add this function to the same module
def list_to_dict(l):
return dict(zip(map(str, range(len(l))), l))
# add this code 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 base
Changes in Pets
Changes in 0
species changed in d2 to cat
Done with changes in 0
Done with changes in Pets
Done with changes in base