递归删除字典中的嵌套项

2024-09-29 23:32:07 发布

您现在位置:Python中文网/ 问答频道 /正文

目前,我正在创建两个列表并比较它们是否重复。
相反,我想递归地从字典中删除嵌套项

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

当前功能:

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

Tags: keyobj列表if字典titleglobalelse
1条回答
网友
1楼 · 发布于 2024-09-29 23:32:07

在迭代列表时修改列表:

我使用了一个列表切片,并根据一个测试函数从原来的列表中排除了项。在

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

范围:

列表切片也解决了这个问题,因为它修改了原始对象。 不过,我还是向每个函数添加了returns,并向每个递归函数调用添加了assignments。因此,分配给的变量从函数中获取返回值,无论返回值的深度如何。在

^{pr2}$

整个文件:

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))

相关问题 更多 >

    热门问题