【问题标题】:Recursive compare of two dictionaries, need help understanding code两个字典的递归比较,需要帮助理解代码
【发布时间】:2014-02-16 01:44:09
【问题描述】:

我不是最擅长python的,我一直在慢慢自学,所以请原谅我缺乏知识。此代码是此处代码的略微修改版本 (Recursive diff of two python dictionaries (keys and values))

我对其 cmets 中列出的函数有几个问题。

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"
                }
            }
        }
    }
}

我必须拿两个字典,比较两者,但我不知道如何处理输出。

【问题讨论】:

  • 看看我的回答:stackoverflow.com/questions/21423384/…>
  • 你知道,只编辑你的整个帖子不是一个好形式。您的问题可能对其他人有用,因此最好保留它。

标签: python recursion dictionary


【解决方案1】:

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

【讨论】:

  • 可能听起来很愚蠢,但我如何使用 difflib 比较两个字符串?我试过“result = list(difflib.Differ.compare(json1_str, json2_str))”但它给了我错误。
  • 看看以下是否适合你:code list(difflib.ndiff(a, b))
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-04
  • 2021-07-29
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 2016-07-08
相关资源
最近更新 更多