从另一个嵌套字典创建嵌套字典

时间:2020-08-07 17:53:44

标签: python

我有一个嵌套的字典,其中包含按类别分组的路径,并且我想创建另一个结构相似的字典,不同之处在于第二个字典将在每个路径中包含文件

原始字典:

dic_paths={
    'folder1':{'data':['C:/Users/my_user/Desktop/Insumos1','C:/Users/my_user/Desktop/Insumos2']},
    'folder2':{'other_data':{'cat1':['C:/Users/my_user/Desktop/DATOS/to_share'],
                        'cat2':['C:/Users/my_user/Desktop/DATOS/others']},
             'other_other_data':{'f2sub-subgroup1':['C:/Users/my_user/Desktop/DATOS/graphs']}}
}

预期结果:

dic_files={
    'folder1':{'data':['list of all files in two paths']},
    'folder2':{'other_data':{'cat1':['list of all files'],
                        'cat2':['list of all files']},
             'other_other_data':{'f2sub-subgroup1':['list of all files']}}
}

当前结果:

dic_files={
    'folder1':'folder1',
    'data':['all files in two paths'],
    'folder2':'folder2',
    'other_data':'other_data',
    'cat1':['list of files'],
    ...
}

这是我正在使用的函数,我从here中获取了原始函数。另外,如何以不重置的方式在函数内部移动data_dic={}?感谢您的帮助

data_dic={}
def myprint(d,data_dic):    
    for k, v in d.items():
        if isinstance(v, dict):
            data_dic[k]=k
            myprint(v,data_dic)
        else:
            file_list=[]
            for path in v:
                if type(path)!=list:                    
                    for file in os.listdir(path):
                        if '~$' not in file:
                            file_list.append(file)
                    data_dic[k]=file_list
    return data_dic

1 个答案:

答案 0 :(得分:1)

您可以申请recursion的完美案例。要遍历文件夹,我使用了Path.iterdir()并使用Path.is_file()检查了每个项目。

代码:

from pathlib import Path

def func(data):
    if isinstance(data, dict):
        return {k: func(v) for k, v in data.items()}  # recursion happens here
    elif isinstance(data, (list, tuple, set, frozenset)):
        return [str(p) for i in data for p in Path(i).iterdir() if p.is_file()]
    else:
        return data  # alternatively you can raise an exception

用法:

dic_paths = {
    'folder1': {
        'data': [
            'C:/Users/my_user/Desktop/Insumos1',
            'C:/Users/my_user/Desktop/Insumos2'
        ]
    },
    'folder2': {
        'other_data': {
            'cat1': ['C:/Users/my_user/Desktop/DATOS/to_share'],
            'cat2':['C:/Users/my_user/Desktop/DATOS/others']
        },
        'other_other_data': {
            'f2sub-subgroup1': ['C:/Users/my_user/Desktop/DATOS/graphs']
        }
    }
}

dic_files = func(dic_paths)