python 按照key 或者value 递归删除 dict中内容

按照value 递归删除dict中符合的项

_filter = [0, '0', None, '']
_key_list = {}

def get_k(data_json, target=_key_list, filter=_filter):
    for key in data_json.keys():
        if isinstance(data_json[key], dict):
            if not bool(data_json[key]):
                continue
            target[key] = {}
            get_k(data_json[key], target[key])
        else:
            if data_json[key] not in filter:
                target[key] = data_json[key]
    return target

 

按照key删除dict中符合的项

#递归删除json/dict中的某个值
new_json={"3":"3","2":2}
def dict_del(key,obj):
    if isinstance(obj,dict):
        if key in obj: obj.pop(key)
        for k,v in obj.items():
            dict_del(key,v)
    if isinstance(obj,list):
        for x in obj:
            dict_del(key,x)
    else: pass
    return obj

del_list=['3','5','6','7','13','14','15','16']
for x in del_list:
    new_json=dict_del(x,new_json)

 

 

 

 

搜索