递归删除字典中的嵌套项

时间:2014-05-19 20:29:27

标签: python python-2.7 recursion dictionary

目前,我正在创建两个列表并将它们作为重复项进行比较 而不是那样,我想以递归方式从字典中删除嵌套项

我的问题是,如何选择深层嵌套的项目并在执行此递归时更改字典?

当前功能:

def _finditem(obj,key):
    if key == 'haha' or key == 'haha1':
        global a_list
        global b_list
    if isinstance(obj,dict):
        _finditem(obj['children'],key)
    else:
        for x in obj:
            if x['title'] == 'Bookmarks Menu':
                _finditem(x['children'],'haha')
            elif x['title'] == 'surf':
                _finditem(x['children'],'haha1')
            else:
                try:
                    _finditem(x['children'],key)
                except:
                    if key == 'haha':
                        a_list.append(x['title'])
                    elif key == 'haha1':
                        b_list.append(x['title'])
                    pass

1 个答案:

答案 0 :(得分:0)

在迭代时修改列表:

我根据测试功能使用了原始列表切片和排除项目。

def discard_func(list_of_dicts):
    list_of_dicts[:] = [x for x in list_of_dicts if test(x)]
    return list_of_dicts

list[:]是整个列表的列表切片语法
Explain Python's slice notation
Remove items from a list while iterating

范围:

列表切片也解决了这个问题,因为它修改了原始对象。 但是,无论如何,我为每个函数和return添加了assignment s到每个递归函数调用。因此,分配给的变量从函数中获取返回的值,无论它有多深。

def _finditem(op_type, obj):
    if isinstance(obj, dict):
        obj['children'] = _finditem(op_type, obj['children'])
    else:
        for x in obj:
            if x['title'] in subjects[op_type]:
                x['children'] = operations[op_type](x['children'])
            else:
                try:
                    x['children'] = _finditem(op_type, x['children'])
                except:
                    continue
    return obj

整个文件:

assign_var = {'compare' : None , 'discard' : [] , 'keep' : [] , 'pairs' : None}
subjects = {'keep' : ['Bookmarks Menu'] , 'discard' : ['surf'] , 'compare' : None , 'pairs' : [ {'keep' : ['Bookmarks Menu'] , 'discard' : ['surf']} , {'keep':'bla','discard':'etc'} ] }
def test(y):
    if 'children' in y:
        if y['title'] not in subjects['keep']:
            discard_func(y['children'])
        else:
            pass
    elif y['title'] in assign_var['keep']:
        print 'Dupicate found'
        return False
    return True
def discard_func(loo):
    loo[:] = [x for x in loo if test(x)]
    return loo
def keep_func(x):
    global assign_var
    for y in x:
        if 'children' in y:
            if y['title'] not in subjects['discard']:
                keep_func(y['children'])
            else:
                continue
        else:
            assign_var['keep'].append(y['title'])
    return x
def _finditem(op_type, obj):
    if isinstance(obj, dict):
        obj['children'] = _finditem(op_type, obj['children'])
    else:
        for x in obj:
            if x['title'] in subjects[op_type]:
                x['children'] = operations[op_type](x['children'])
            else:
                try:
                    x['children'] = _finditem(op_type, x['children'])
                except:
                    continue
    return obj
operations = { 'keep' : keep_func , 'discard' : discard_func , 'pairs' : None , 'compare' : None }
def parent_function():
    op_type = 'keep'
    _finditem(op_type, book)
    op_type = 'discard'
    book_new = _finditem(op_type, book)
    # for op_type in assign_var:
    #   try:
    #       _finditem(op_type = op_type, obj = book)
    #   except:
    #       pass
    return book_new
if __name__ == '__main__':
    print 'In __main__'
    import json
    loc = 'C:\\workspace\\temp\\'
    with open(loc + 'bookmarks-2014-05-24.json', 'r') as nctn:
        book = json.load(nctn)
    book_new = parent_function()
    with open(loc + 'bookmarks-2014-05-24_new.json', 'w') as nctn:
        nctn.write(json.dumps(book_new))
相关问题