首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >递归字典列表等(python)

递归字典列表等(python)
EN

Stack Overflow用户
提问于 2012-09-21 22:36:55
回答 2查看 263关注 0票数 0

我有一个从yaml配置文件返回的字典,它有4个级别:

项目、部分、字段、元素

代码语言:javascript
复制
{
    "tag": "test", 
    "sections": [
        {
            "info": "This is section ONE", 
            "tag": "s1"
        }, 
        {
            "info": "This is section TWO", 
            "fields": [
                {
                    "info": "This is field ONE", 
                    "tag": "f1"
                }, 
                {
                    "info": "This is field TWO", 
                    "tag": "f2", 
                    "elements": [
                        {
                            "info": "This is element", 
                            "tag": "e1", 
                            "type_of": "text_field"
                        }, 
                        {
                            "info": "This is element", 
                            "tag": "e2", 
                            "type_of": "text_field"
                        }, 
                        {
                            "info": "This is element", 
                            "tag": "e3", 
                            "type_of": "text_field"
                        }, 
                        {
                            "info": "This is element", 
                            "tag": "e4", 
                            "type_of": "text_field"
                        }
                    ]
                }, 
                {
                    "info": "This is field THREE", 
                    "tag": "f3", 
                    "elements": [
                        {
                            "info": "This is element", 
                            "tag": "e5", 
                            "type_of": "text_field"
                        }, 
                        {
                            "info": "This is element", 
                            "tag": "e6", 
                            "type_of": "text_field"
                        }, 
                        {
                            "info": "This is element", 
                            "tag": "e7", 
                            "type_of": "text_field"
                        }, 
                        {
                            "info": "This is element ONE", 
                            "tag": "e8", 
                            "type_of": "text_field"
                        }
                    ]
                }
            ], 
            "tag": "s2"
        }, 
        {
            "info": "This is section THREE", 
            "fields": [
                {
                    "info": "This is field FOUR", 
                    "tag": "f4"
                }, 
                {
                    "info": "This is field FIVE", 
                    "tag": "f5"
                }, 
                {
                    "info": "This is field SIX", 
                    "tag": "f6"
                }
            ], 
            "tag": "s3"
        }
    ],
    "type_of": "custom"
}

代码语言:javascript
复制
class T():

    def __init__(self):
        self.sections = []
        self.fields = []
        self.elements = []

def rt(y):
    t = T()

    def recurse(y):
        for k,v in y.iteritems(): 
            if isinstance(v, list):
                getattr(t, k).append(v)
                [recurse(i) for i in v]
            else:
                setattr(t, k, v)
    recurse(y)
    return t

因此,我需要递归一个字典列表,其中包含字典列表等。al。将它们按类型分类(然后添加对其所属部分的引用,但一次只有一个问题)。并将其放入T。

这是可行的,但不会删除任何内容,即每个部分都会被捕获,但所有其他部分(字段、元素)都会被捕获。这可能是comp sci 101,但我主要是自学,所以这是我需要学习的排序算法。任何关于改进这一点的意见都将受到欢迎。

编辑:事实证明,这比我预期的更深入,抽象地说,这是一个学习如何遍历任意数据结构并挑选出我想要或需要的东西的机会

EN

Stack Overflow用户

回答已采纳

发布于 2012-09-26 23:41:20

我想出的解决方案是:

代码语言:javascript
复制
class Tree:
    def __init__(self, node, cargo, parent=None):
        self.node = node
        self.cargo = cargo
        self.parent  = parent
    def __str__(self):
        return str(self.cargo)

from copy import copy
def just_part(y):
    z = copy(y)
    for k,v in z.items():
        if isinstance(v, list):
            del z[k]
    return z

def rt(y):
    tt = []
    s = Tree( id(y), just_part(y) )
    tt.append(s)
    def recurse(y):
        for k,v in y.iteritems(): 
            if isinstance(v, list):
                [tt.append( Tree(id(i), just_part(i), id(y) ) ) for i in v]
                [recurse(i) for i in v]
            else:
                pass
    recurse(y)
    return tt

我只是运行rt(我的嵌套字典),这会返回一个节点列表,到目前为止,这个列表对于我正在做的事情来说已经足够了,而且我知道它可以以一种更有效的方式完成。也有这样的:http://code.activestate.com/recipes/577982-recursively-walk-python-objects/,但它不能处理所有问题,而我自己的解决方案可能不是目前最有效的pythonic解决方案。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12532670

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档