压栈

data = {"time":"2016-08-05T13:13:05",
        "some_id":"ID1234",
        "grp1":{"fld1":1,"fld2":2},
        "xxx2":{"fld3":0,"fld5":0.4},
        "fld6":11,
        "fld7":7,
        "fld46":8}

li = [data]                 #压栈思维,就是把处理好的收起来,不能处理的,放到一个容器li[]里再来一遍
fields = 'fld2|fld3|fld7|fld19'
result = {}
def select(data,fields):
    fields_li = fields.split('|')
    while li:
        dic = li.pop()
        for key in dic:
            if type(dic[key]) == dict:
                li.append(dic[key])
            elif key in fields_li:
                result[key] = dic[key]
    return result
r = select(data,fields)
print(r)

 

posted @ 2017-10-18 18:25  静静别跑  阅读(224)  评论(0编辑  收藏  举报