两个字典的递归比较,需要帮助理解代码

时间:2014-01-24 21:58:38

标签: python recursion dictionary

我不是蟒蛇最伟大的人,我一直在慢慢地教自己,所以请原谅我缺乏知识。此代码是此处(Recursive diff of two python dictionaries (keys and values)

中代码的略微修改版本

我对其评论中列出的功能有几个问题。

def dd(d1, d2): 
    for k in d1: 
        if k not in d2: ##if the key from dict1 is not in dict2
            print k + " removed from d2"
    for k in d2:
        if k not in d1: ##if the key from dict2 is not in dict 1
            print k + " added in d2"
            continue
        if d2[k] != d1[k]: ## if dict 2 values are not the same as the values in dict 1
            if type(d2[k]) not in (dict, list): ## ???
                print k + " changed in d2 to " + str(d2[k])
            else:
                if type(d1[k]) != type(d2[k]): ## ???
                    print k + " changed to " + str(d2[k])
                    continue
                else:
                    if type(d2[k]) == dict: ## if dict2 values are in dict format?? 
                        dd(d1[k], d2[k]) ## pass dict1 / dict 2 values through the function again,  what is the purpose of this?
                        continue
    return

我还有另外一个关于代码输出的问题。如果我使用以下代码作为d1。

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
                    "ID": "SGML",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": [
                            "GML",
                            "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

将ID的值更改为“1234”并将该文件用作d2。 dd(d1,d2)将输出:

"ID changed in d2 to 1234"

这正是我对代码的期望(密钥+“在d2中更改为”+值)

我想尝试将输出更改为类似于更新github上的文件时的输出,因此更容易看到更改的内容以及如下所示:

{
    "glossary": {
        "title": "example glossary",
        "GlossDiv": {
            "title": "S",
            "GlossList": {
                "GlossEntry": {
-                   "ID": "SGML",
+                   "ID": "1234",
                    "SortAs": "SGML",
                    "GlossTerm": "Standard Generalized Markup Language",
                    "Acronym": "SGML",
                    "Abbrev": "ISO 8879:1986",
                    "GlossDef": {
                        "para": "A meta-markup language, used to create markup languages such as DocBook.",
                        "GlossSeeAlso": [
                            "GML",
                            "XML"
                        ]
                    },
                    "GlossSee": "markup"
                }
            }
        }
    }
}

我必须拿两个词典,比较两个词典,但我不知道如何接近输出。

1 个答案:

答案 0 :(得分:0)

尝试查看'difflib'(http://docs.python.org/2/library/difflib.html) 您可以在字典的字符串/漂亮打印表示上运行比较。