如何拆分字符串并使用 python 定义 parents 和 child?

How to split a string and define parents and child using python?

我有一个 JSON 文件,如下所示:

{
    "Example0/Example1/File1.py": {
        "Example0/Element_1/File1.py": null,
        "ExampleA/ExampleB/File2.py": 0.0769,
    },
    "ExampleA/ExampleB/File2.py": {
        "Example0/Example1/File1.py": 1.0,
        "ExampleA/ExampleB/File2.py": null,
    }
}

直到现在我只需要从文件夹中拆分“Files.py”,但作为一项新任务,每次 "/" 出现时我都必须拆分它并存储它以便我可以将此信息写为 XML 文件的一个参数,其中列出了每个元素,例如“File1.py”将是“Example1”的 child,这将是 child 的“示例 0”。

到目前为止我使用的代码是这样的:

arch = {}
for key in json_dict:
    splitted = key.rsplit("/", 1)
    parent = splitted[0]
    child = key
    if parent in arch:
        arch[parent].append(child)
    else:
        arch[parent] = [child]

我们的想法是更改代码以产生如下输出:

parent0 = Example0
parent1 = Example0/Example1
child = key

但不限于两个parents,因为可以有类似的东西:

ExampleA/ExampleB/ExampleC/ExampleD/File0.py

预期输出的位置:

parent0 = ExampleA
parent1 = ExampleA/ExampleB
parent2 = ExampleA/ExampleB/ExampleC
parent3 = ExampleA/ExampleB/ExampleC/ExampleD

child = key

我不完全确定您的预期输出应该是什么样子:

where every element is listed and, for example "File1.py" would be a child of "Example1" and this would be a child of "Example0"

Where the expected output would be:

parent0 = ExampleA
parent1 = ExampleA/ExampleB
parent2 = ExampleA/ExampleB/ExampleC
parent3 = ExampleA/ExampleB/ExampleC/ExampleD

child = key

好像有点不一样?最好的办法是在问题中包含示例的明确预期输出 - 即预期的 arch 字典。

这里有一个建议:

from collections import defaultdict
from pathlib import PurePath as Path

arch = defaultdict(list)
for key in json_dict:
    for path in list(Path(key).parents)[:-1]:
        arch[str(path)].append(key)

的输出
json_dict = {
    "Ex0/F1.py": {"Some content": None,},
    "Ex0/Ex1/F1.py": {"Some content": None,},
    "ExA/ExB/F2.py": {"Some content": 1.0,},
    "ExA/ExB/ExC/ExD/F0.py": {"Some content": None}
}

会是

{'Ex0': ['Ex0/F1.py', 'Ex0/Ex1/F1.py'],
 'Ex0/Ex1': ['Ex0/Ex1/F1.py'],
 'ExA': ['ExA/ExB/F2.py', 'ExA/ExB/ExC/ExD/F0.py'],
 'ExA/ExB': ['ExA/ExB/F2.py', 'ExA/ExB/ExC/ExD/F0.py'],
 'ExA/ExB/ExC': ['ExA/ExB/ExC/ExD/F0.py'],
 'ExA/ExB/ExC/ExD': ['ExA/ExB/ExC/ExD/F0.py']}

如果您不喜欢使用 Path 那么这应该会产生相同的输出:

from collections import defaultdict

arch = defaultdict(list)
for key in json_dict:
    path = key
    while True:
        if "/" not in path:
            break
        path = path.rsplit("/", maxsplit=1)[0]
        arch[path].append(key)

这是您要找的吗?还是更像

from collections import defaultdict
from pathlib import PurePath as Path

arch = defaultdict(list)
for key in json_dict:
    parents = [key] + list(Path(key).parents)[:-1]
    for child, parent in zip(parents[:-1], parents[1:]):
        arch[str(parent)].append(str(child))
{'Ex0': ['Ex0/F1.py', 'Ex0/Ex1'],
 'Ex0/Ex1': ['Ex0/Ex1/F1.py'],
 'ExA/ExB': ['ExA/ExB/F2.py', 'ExA/ExB/ExC'],
 'ExA': ['ExA/ExB', 'ExA/ExB'],
 'ExA/ExB/ExC/ExD': ['ExA/ExB/ExC/ExD/F0.py'],
 'ExA/ExB/ExC': ['ExA/ExB/ExC/ExD']}

...?