给定一些任意字典
mydict = {
'first': {
'second': {
'third': {
'fourth': 'the end'
}
}
}
}
我写了一个小例程来在writing an answer的过程中压平它。另一个问题。
def recursive_flatten(mydict):
d = {}
for k, v in mydict.items():
if isinstance(v, dict):
for k2, v2 in recursive_flatten(v).items():
d[k + '.' + k2] = v2
else:
d[k] = v
return d
它有效,给了我我想要的:
new_dict = recursive_flatten(mydict)
print(new_dict)
{'first.second.third.fourth': 'the end'}
并且应该适用于任何任意结构的字典。不幸的是,它没有:
mydict['new_key'] = mydict
现在recursive_flatten(mydict)
将运行,直到我用完堆栈空间。我试图找出如何优雅地处理 self 引用(基本上,忽略或删除它们)。使事情复杂化的是,任何子词典都可能发生自引用......而不仅仅是顶级词典。我如何优雅地处理 self 引用?我可以想到一个可变的默认参数,但是应该有更好的方法......对吧?
感谢指点,感谢阅读。如果您有的话,我欢迎对 recursive_flatten
提出任何其他建议/改进。
最佳答案
一种方法是使用set
来做到这一点和 id
。请注意,此解决方案还使用生成器,这意味着我们可以在计算整个结果之前开始使用扁平化字典
def recursive_flatten (mydict):
def loop (seen, path, value):
# if we've seen this value, skip it
if id(value) in seen:
return
# if we haven't seen this value, now we have
else:
seen.add(id(value))
# if this value is a dict...
if isinstance (value, dict):
for (k, v) in value.items ():
yield from loop(seen, path + [k], v)
# base case
else:
yield (".".join(path), value)
# init the loop
yield from loop (set(), [], mydict)
程序演示
mydict = {
'first': {
'second': {
'third': {
'fourth': 'the end'
}
}
}
}
for (k,v) in recursive_flatten (mydict):
print (k, v)
# first.second.third.fourth the end
mydict['new_key'] = mydict
for (k,v) in recursive_flatten (mydict):
print (k, v)
# first.second.third.fourth the end
如果您想查看自引用值的输出,我们可以稍作修改
# if we've seen this value, skip it
if (id(value) in seen):
# this is the new line
yield (".".join(path), "*self-reference* %d" % id(value))
return
现在程序的输出将是
first.second.third.fourth the end
first.second.third.fourth the end
new_key *self-reference* 139700111853032
关于python - 展平字典时处理自引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49946398/