Python (3.4) 递归函数在字典/树扁平化时不被调用



我正在尝试扁平化嵌套字典,同时根据另一个嵌套字典交换其键。 我假设没有一个分支具有相同的键。 例如:

在:

values_dict_test = {"abs": 3, "cd": 23, "sdf": "abc", "gxr":
{"rea": 21, "bdf": 95}}
mapping_dict_test = {"abs": "one", "cd": "two", "sdf": "three", "gxr":
{"rea": "four", "bdf": "five"}}

预期出局:

{"one": 3, "two": 23, "three": "abc", "four": 21, "five": 95}

我正在使用iteritems黑客来尝试使此代码与Python 2.7兼容,但正在3.4上进行测试。 我添加了一堆打印语句来跟踪执行;递归调用似乎从未真正发生过。

try:
    dict.iteritems
except AttributeError:
    # Python 3
    def itervalues(d):
        return iter(d.values())
    def iteritems(d):
        return iter(d.items())
else:
    # Python 2
    def itervalues(d):
        return d.itervalues()
    def iteritems(d):
        return d.iteritems()
def flatten_dict(mapping, values_dict):
    print("Function called with {} and {}".format(mapping, values_dict))
    for (key, value) in iteritems(mapping):
        print("K: {} V: {} Mapping: {}".format(key, value, mapping))
        if isinstance(value, dict):
            # Note that we have to flatten the values_dict as we flatten
            # mapping dict, hence the [key]
            print("Going to recurse")
            print("Passing {} and {}".format(value, values_dict[key]))
            flatten_dict(value, values_dict[key])
        else:
            print("Going to yield {}, {}".format(value, values_dict[key]))
            yield (value, values_dict[key])
values_dict_test = {"abs": 3, "cd": 23, "sdf": "abc", "gxr":
{"rea": 21, "bdf": 95}}
mapping_dict_test = {"abs": "one", "cd": "two", "sdf": "three", "gxr":
{"rea": "four", "bdf": "five"}}
for (x,y) in flatten_dict(mapping_dict_test, values_dict_test):
    print(x, y)

输出:

Function called with {'cd': 'two', 'sdf': 'three', 'abs': 'one', 'gxr': {'rea': 'four', 'bdf': 'five'}} and {'cd': 23, 'sdf': 'abc', 'abs': 3, 'gxr': {'rea': 21, 'bdf': 95}}
K: cd V: two Mapping: {'cd': 'two', 'sdf': 'three', 'abs': 'one', 'gxr': {'rea': 'four', 'bdf': 'five'}}
Going to yield two, 23
two 23
K: sdf V: three Mapping: {'cd': 'two', 'sdf': 'three', 'abs': 'one', 'gxr': {'rea': 'four', 'bdf': 'five'}}
Going to yield three, abc
three abc
K: abs V: one Mapping: {'cd': 'two', 'sdf': 'three', 'abs': 'one', 'gxr': {'rea': 'four', 'bdf': 'five'}}
Going to yield one, 3
one 3
K: gxr V: {'rea': 'four', 'bdf': 'five'} Mapping: {'cd': 'two', 'sdf': 'three', 'abs': 'one', 'gxr': {'rea': 'four', 'bdf': 'five'}}
Going to recurse
Passing {'rea': 'four', 'bdf': 'five'} and {'rea': 21, 'bdf': 95}

(通常,我会使用 values = {key: value for (key, value) in values_dict_gen}其中values_dict_gen是该函数返回的生成器。

编辑:赞成重新打开,(1)链接到的副本使用可变的默认参数,已知在这些情况下的行为与直觉相反(2)它较旧,下面给出的答案显示了我在旧问题上没有看到的Python 3.3解决方案。

flatten_dict(value, values_dict[key])

此调用在生成器内部执行与在生成器外部相同的操作;它创建一个生成器对象。它不会自动运行生成器并生成其项目。如果你想递归地使用生成器,你必须自己迭代它:

for item in flatten_dict(value, values_dict[key]):
    yield item

或在 Python 3 中:

yield from flatten_dict(value, values_dict[key])

最新更新