展平字典时处理自引用 [英] Handle self-references when flattening dictionary

查看:47
本文介绍了展平字典时处理自引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一些任意字典

mydict = {
    'first': {
        'second': {
            'third': {
                'fourth': 'the end'
             }
         }
     }
}

写另一个问题的答案的过程中,我编写了一个小例程来使其扁平化.>

I've written a small routine to flatten it in the process of writing an answer to another question.

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

它起作用了,给了我我想要的东西:

It works, giving me what I want:

new_dict = recursive_flatten(mydict)

print(new_dict)
{'first.second.third.fourth': 'the end'}

应该适用于任何任意结构的字典.不幸的是,它没有:

And should work for just about any arbitrarily structured dictionary. Unfortunately, it does not:

mydict['new_key'] = mydict

现在 recursive_flatten(mydict)将一直运行,直到堆栈空间用完.我试图弄清楚如何正常处理自我引用(基本上是忽略或删除它们).使事情复杂化的是,任何子词典都可能出现自我引用,而不仅仅是顶层.我将如何优雅地处理自我推荐?我可以想到一个可变的默认参数,但是应该应该是更好的方法...对吗?

Now recursive_flatten(mydict) will run until I run out of stack space. I'm trying to figure out how to gracefully handle self-references (basically, ignore or remove them). To complicate matters, self-references may occur for any sub-dictionary... not just the top level. How would I handle self-references elegantly? I can think of a mutable default argument, but there should be a better way... right?

指针感谢,感谢您的阅读.如果您有任何其他对 recursive_flatten 的建议/改进,我将表示欢迎.

Pointers appreciated, thanks for reading. I welcome any other suggestions/improvements to recursive_flatten if you have them.

推荐答案

使用 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

如果您想查看自参考值的输出,我们可以做些修改

We can make a slight modification if you would like to see output for self-referential values

# 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

这篇关于展平字典时处理自引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆