合并dicts不能按预期工作python 3.5

时间:2017-07-26 20:15:04

标签: python dictionary python-3.5

这是Python 3.5环境,我认为代码是自解释的,在这里,我希望这两个函数都能正常工作,但只有一个是正确的。

TLDR: 分配out = {**out, **answer}会使out变量包含右键,但在接下来的两个步骤中会丢失;虽然分配c = {**c,**a}在测试功能中完美运行,但新密钥不会丢失。

有人可以解释一下我做错了吗?

def flatify_works(d, out, fhook=None):
    for k, v in d.items():
        if not isinstance(v, dict) and not isinstance(v, list):
            out[k] = v
        elif isinstance(v, dict):
            flatify_works(v, out, fhook)
        elif isinstance(v, list):
            if fhook is None:
                raise AssertionError("an array with more than 1 elment found.")
            answer = fhook(k, v)
            for k, v in answer.items():
                out[k] = v


def flatify_doesnt_work(d, out, fhook=None):
    for k, v in d.items():
        if not isinstance(v, dict) and not isinstance(v, list):
            out[k] = v
        elif isinstance(v, dict):
            flatify_doesnt_work(v, out, fhook)
        elif isinstance(v, list):
            if fhook is None:
                raise AssertionError("an array with more than 1 elment found.")
            answer = fhook(k, v)
            out = {**out, **answer}  # put a breakpoint here, and go 2 steps further


def hook(k, v):
    l = [d["c"] for d in v]
    return {"c": sum(l), "d": "blabla"}


def test_merge_dicts():
    a = {"a": 1, "b": 2}
    c = {"c": 3}
    c = {**c, **a}  # merging works perfectly here
    print(c)
    assert "a" in c and "b" in c and "c" in c  # ok


def test_nested_works():
    out = {}
    flatify_works({"a": 1, "b": [{"c": 0.6, "d": 4}, {"c": 0.4, "d": 4}]}, out, hook)
    print("working exemple: {}".format(str(out)))


def test_nested_doesnt_work():
    out = {}
    flatify_doesnt_work({"a": 1, "b": [{"c": 0.6, "d": 4}, {"c": 0.4, "d": 4}]}, out, hook)
    print("not working exemple: {}".format(str(out)))


def main():
    test_merge_dicts()  # ok
    test_nested_works() # ok
    test_nested_doesnt_work() # why out = {**out, **answer} is not working as expected?


if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

我认为区别在于,在第二个函数中,您重新分配out变量,然后将其与传递给函数的dict分离:

out = {**out, **answer} # out is now a different object

由于函数是递归的,后续修改不会影响原始字典。

在第一个函数中,您只进行项目分配,它会按预期修改dict:

out[k] = v