gpt4 book ai didi

python - 将文件列表转换为树状字典

转载 作者:行者123 更新时间:2023-12-02 09:35:36 26 4
gpt4 key购买 nike

假设我有一个如下所示的列表:

list_all_files = [['folder1', 'subfolder1', 'file1'], 
['folder1', 'subfolder1', 'file2'],
['folder1', 'subfolder1', 'file3'],
['folder1', 'subfolder1', 'file4'],
['folder1', 'subfolder2', 'file1'],
['folder1', 'subfolder2', 'file2'],
['folder2', 'subfolder1', 'file1'],
['folder2', 'subfolder1', 'file2'],
['folder3', 'file1'],
['folder3', 'file2'],
['folder4', 'subfolder1', 'file1'],
['folder4', 'subfolder1', 'file2'],
['folder2', 'subfolder2', 'file1'],
['folder2', 'subfolder2', 'file2'],
['folder2', 'subfolder2', 'file3'],
['folder2', 'subfolder2', 'file4']]

“list_all_files”只是一个示例 - 该列表还可以有零个或 n 个文件夹和/或子文件夹和/或文件。如何将其转换为如下所示的字典?

dict_all_files =

{ 'folder1': {'subfolder1': {'file1', 'file2', 'file3', 'file4'},
'subfolder2': {'file1', 'file2'}},
'folder2': {'subfolder1': {'file1', 'file2'},
'subfolder2': {'file1', 'file2', 'file3', 'file4'}},
'folder3': {'file1', 'file2'},
'folder4': {'subfolder1': {'file1', 'file2'}} }

我尝试循环列表并使用 dict.update(),如下所示:

dict_all_files = {}
for member in list_all_files:
if member[0] == 'folder1':
dict_all_files.update({'folder1': ''})
for element in member:
if member[1] == 'subfolder1':
dict_all_files.update({folder1': member[1]})

但随后我会覆盖文件夹,而且我还必须手动为每个文件夹和子文件夹编写 if 语句,这不太实用。因此,对我的代码进行处理是没有意义的,因为它已经存在缺陷。或许我从一开始就想错了?如果有人可以提供答案或至少提供提示,那就太好了。我没有找到任何回答此问题或类似问题的问题。

最佳答案

您可以使用dict.setdefault来清理代码。

import pprint
list_all_files = [['folder1', 'subfolder1', 'file1'],
['folder1', 'subfolder1', 'file2'],
['folder1', 'subfolder1', 'file3'],
['folder1', 'subfolder1', 'file4'],
['folder1', 'subfolder2', 'file1'],
['folder1', 'subfolder2', 'file2'],
['folder2', 'subfolder1', 'file1'],
['folder2', 'subfolder1', 'file2'],
['folder3', 'file1'],
['folder3', 'file2'],
['folder4', 'subfolder1', 'file1'],
['folder4', 'subfolder1', 'file2'],
['folder2', 'subfolder2', 'file1'],
['folder2', 'subfolder2', 'file2'],
['folder2', 'subfolder2', 'file3'],
['folder2', 'subfolder2', 'file4']]

result = {}
for path in list_all_files:
head = result
for name in path[:-2]:
head = head.setdefault(name,{})
head.setdefault(path[-2],set()).add(path[-1])

pprint.pprint(result)

输出

{'folder1': {'subfolder1': set(['file1', 'file2', 'file3', 'file4']),
'subfolder2': set(['file1', 'file2'])},
'folder2': {'subfolder1': set(['file1', 'file2']),
'subfolder2': set(['file1', 'file2', 'file3', 'file4'])},
'folder3': set(['file1', 'file2']),
'folder4': {'subfolder1': set(['file1', 'file2'])}}

关于python - 将文件列表转换为树状字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60935433/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com