将路径列表转换为 python 中的字典
Convert list of paths to dictionary in python
我正在 Python 中制作一个程序,我需要在其中与 "hypothetical" 路径进行交互(也就是在实际文件系统中不存在也不会存在的路径)并且我需要能够像往常一样 listdir
它们(path['directory']
会 return 目录中的每个项目,如 os.listdir()
)。
我想到的解决方案是将字符串路径列表转换为字典字典。我想出了这个递归函数(它在 class 内):
def DoMagic(self,paths):
structure = {}
if not type(paths) == list:
raise ValueError('Expected list Value, not '+str(type(paths)))
for i in paths:
print(i)
if i[0] == '/': #Sanity check
print('trailing?',i) #Inform user that there *might* be an issue with the input.
i[0] = ''
i = i.split('/') #Split it, so that we can test against different parts.
if len(i[1:]) > 1: #Hang-a-bout, there's more content!
structure = {**structure, **self.DoMagic(['/'.join(i[1:])])}
else:
structure[i[1]] = i[1]
但是当我使用 ['foo/e.txt','foo/bar/a.txt','foo/bar/b.cfg','foo/bar/c/d.txt']
作为输入转到 运行 时,我得到:
{'e.txt': 'e.txt', 'a.txt': 'a.txt', 'b.cfg': 'b.cfg', 'd.txt': 'd.txt'}
我希望能够 path['foo']['bar']
获取 foo/bar/
目录中的所有内容。
编辑:
更理想的输出是:
{'foo':{'e.txt':'e.txt','bar':{'a.txt':'a.txt','c':{'d.txt':'d.txt'}}}}
通过字典实现的简单树还不够吗?
您的实施似乎有点多余。很难轻易分辨文件属于哪个文件夹。
https://en.wikipedia.org/wiki/Tree_(data_structure)
pypi
上有很多库,如果您需要额外的东西。
treelib
pathlib中还有Pure paths
。
这个怎么样。它可以获得您想要的输出,但是树结构可能更清晰。
from collections import defaultdict
import json
def nested_dict():
"""
Creates a default dictionary where each value is an other default dictionary.
"""
return defaultdict(nested_dict)
def default_to_regular(d):
"""
Converts defaultdicts of defaultdicts to dict of dicts.
"""
if isinstance(d, defaultdict):
d = {k: default_to_regular(v) for k, v in d.items()}
return d
def get_path_dict(paths):
new_path_dict = nested_dict()
for path in paths:
parts = path.split('/')
if parts:
marcher = new_path_dict
for key in parts[:-1]:
marcher = marcher[key]
marcher[parts[-1]] = parts[-1]
return default_to_regular(new_path_dict)
l1 = ['foo/e.txt','foo/bar/a.txt','foo/bar/b.cfg','foo/bar/c/d.txt', 'test.txt']
result = get_path_dict(l1)
print(json.dumps(result, indent=2))
输出:
{
"foo": {
"e.txt": "e.txt",
"bar": {
"a.txt": "a.txt",
"b.cfg": "b.cfg",
"c": {
"d.txt": "d.txt"
}
}
},
"test.txt": "test.txt"
}
我正在 Python 中制作一个程序,我需要在其中与 "hypothetical" 路径进行交互(也就是在实际文件系统中不存在也不会存在的路径)并且我需要能够像往常一样 listdir
它们(path['directory']
会 return 目录中的每个项目,如 os.listdir()
)。
我想到的解决方案是将字符串路径列表转换为字典字典。我想出了这个递归函数(它在 class 内):
def DoMagic(self,paths):
structure = {}
if not type(paths) == list:
raise ValueError('Expected list Value, not '+str(type(paths)))
for i in paths:
print(i)
if i[0] == '/': #Sanity check
print('trailing?',i) #Inform user that there *might* be an issue with the input.
i[0] = ''
i = i.split('/') #Split it, so that we can test against different parts.
if len(i[1:]) > 1: #Hang-a-bout, there's more content!
structure = {**structure, **self.DoMagic(['/'.join(i[1:])])}
else:
structure[i[1]] = i[1]
但是当我使用 ['foo/e.txt','foo/bar/a.txt','foo/bar/b.cfg','foo/bar/c/d.txt']
作为输入转到 运行 时,我得到:
{'e.txt': 'e.txt', 'a.txt': 'a.txt', 'b.cfg': 'b.cfg', 'd.txt': 'd.txt'}
我希望能够 path['foo']['bar']
获取 foo/bar/
目录中的所有内容。
编辑:
更理想的输出是:
{'foo':{'e.txt':'e.txt','bar':{'a.txt':'a.txt','c':{'d.txt':'d.txt'}}}}
通过字典实现的简单树还不够吗? 您的实施似乎有点多余。很难轻易分辨文件属于哪个文件夹。
https://en.wikipedia.org/wiki/Tree_(data_structure)
pypi
上有很多库,如果您需要额外的东西。
treelib
pathlib中还有Pure paths
。
这个怎么样。它可以获得您想要的输出,但是树结构可能更清晰。
from collections import defaultdict
import json
def nested_dict():
"""
Creates a default dictionary where each value is an other default dictionary.
"""
return defaultdict(nested_dict)
def default_to_regular(d):
"""
Converts defaultdicts of defaultdicts to dict of dicts.
"""
if isinstance(d, defaultdict):
d = {k: default_to_regular(v) for k, v in d.items()}
return d
def get_path_dict(paths):
new_path_dict = nested_dict()
for path in paths:
parts = path.split('/')
if parts:
marcher = new_path_dict
for key in parts[:-1]:
marcher = marcher[key]
marcher[parts[-1]] = parts[-1]
return default_to_regular(new_path_dict)
l1 = ['foo/e.txt','foo/bar/a.txt','foo/bar/b.cfg','foo/bar/c/d.txt', 'test.txt']
result = get_path_dict(l1)
print(json.dumps(result, indent=2))
输出:
{
"foo": {
"e.txt": "e.txt",
"bar": {
"a.txt": "a.txt",
"b.cfg": "b.cfg",
"c": {
"d.txt": "d.txt"
}
}
},
"test.txt": "test.txt"
}