如何从Python字典中创建所有可能的路径?

huangapple go评论166阅读模式
英文:

How to create all paths possible from a python dictionary?

问题

我有一个Python字典,我想要输出所有键值的所有可能路径。以下是一个小规模示例,可视化说明我想要做的事情。

dictionary = {'parent':['child1','child2'], 'child1': ['child1_1','child1_2'], 'child2': ['child2_1','child2_2'], 'child3': [], 'child1_1': ['child1_1_1', 'child1_1_2'], 'child1_1_1': [], 'child1_1_2': [], 'child1_2': [], 'child2_1': [], 'child2_2': [], 'child4':[]}

我想要的输出如下:

parent/child1
parent/child1/child1_1
parent/child1/child1_1/child1_1_1
parent/child1/child1_1/child1_1_2
parent/child1/child1_2
parent/child2/child2_1
parent/child2/child2_2
parent/child3
parent/child4
.
.
.

请注意,我想要用于更大规模的情况,因此使用两个循环,我能够输出一个包含父项和其两个直接子项的路径。但是对于更大规模,这种方法不起作用,我认为我需要在一个while true循环内使用for循环,以便我可以检查子项是否没有自己的子项,并输出“嘿,我是最后一个,这是对我可用的路径”等等。

提前感谢您,祝您有美好的一天。

英文:

I have a python dictionary where I'd like to output all possible paths from all key values. Here is a small scale example to visualize what I'm trying to do.

dictionary = {'parent':['child1','child2'], ‘child1’: ['child1_1','child1_2'], ‘child2’: ['child2_1','child2_2'], ‘child3’: [], 'child1_1'= ['child1_1_1', 'child1_1_2'], 'child1_1_1': [], 'child1_1_2': [], 'child1_2': [], 'child2_1': [], 'child2_2': [], 'child4'=[]}

And the output that I'd like to have is something like this:

parent/child1

parent/child1/child1_1

parent/child1/child1_1/child1_1_1

parent/child1/child1_1/child1_1_2

parent/child1/child1_2

parent/child2/child2_1

parent/child2/child2_2

parent/child3

parent/child4

.

.

.

Please note that I'd like to use it for a larger scale, so using 2 for loops I was able to output a path with parent and a 2 direct childs of it. But it doesn't work on larger scale, I think I need a for loop inside of a while true loop where I can check if a child doesn't have any childs of itself and it outputs me “Hey I'm the last one left and here is paths that are available to me" etc.

Thanks in advance and have a nice day.

答案1

得分: 3

child3和child4没有在父级中提到,所以如果我们忽略掉这一点,您希望在输出中指向父级,那么提供所需输出的函数如下:

def get_paths(dictionary, parent="", paths=None):
    if paths is None:
        paths = []

    paths.append(parent)

    if parent in dictionary:
        children = dictionary[parent]
        for child in children:
            child_paths = get_paths(dictionary, child)
            paths.extend([f"{parent}/{path}" for path in child_paths])

    return paths


dictionary = {
    'parent': ['child1', 'child2'],
    'child1': ['child1_1', 'child1_2'],
    'child2': ['child2_1', 'child2_2'],
    'child3': [],
    'child1_1': ['child1_1_1', 'child1_1_2'],
    'child1_1_1': [],
    'child1_1_2': [],
    'child1_2': [],
    'child2_1': [],
    'child2_2': [],
    'child4': [],
}

paths = get_paths(dictionary, 'parent')

for path in paths:
    print(path)

输出:

parent
parent/child1
parent/child1/child1_1
parent/child1/child1_1/child1_1_1
parent/child1/child1_1/child1_1_2
parent/child1/child1_2
parent/child2
parent/child2/child2_1
parent/child2/child2_2
英文:

child3 and child4 are not mentioned in the parent, so how do you want to point to parent in the output, if we ignore that the function that gives you desired output is this:

def get_paths(dictionary, parent="", paths=None):
    if paths is None:
        paths = []

    paths.append(parent)

    if parent in dictionary:
        children = dictionary[parent]
        for child in children:
            child_paths = get_paths(dictionary, child)
            paths.extend([f"{parent}/{path}" for path in child_paths])

    return paths


dictionary = {
    'parent': ['child1', 'child2'],
    'child1': ['child1_1', 'child1_2'],
    'child2': ['child2_1', 'child2_2'],
    'child3': [],
    'child1_1': ['child1_1_1', 'child1_1_2'],
    'child1_1_1': [],
    'child1_1_2': [],
    'child1_2': [],
    'child2_1': [],
    'child2_2': [],
    'child4': [],
}

paths = get_paths(dictionary, 'parent')

for path in paths:
    print(path)

Output:

parent
parent/child1
parent/child1/child1_1
parent/child1/child1_1/child1_1_1
parent/child1/child1_1/child1_1_2
parent/child1/child1_2
parent/child2
parent/child2/child2_1
parent/child2/child2_2

答案2

得分: 2

你可以使用递归函数来实现这个。

d = {'parent':['child1','child2'], 'child1': ['child1_1','child1_2'], 'child2': ['child2_1','child2_2'], 'child3': [], 'child1_1': ['child1_1_1', 'child1_1_2'], 'child1_1_1': [], 'child1_1_2': [], 'child1_2': [], 'child2_1': [], 'child2_2': [], 'child4': []}
                                                                                
def print_children(parent, parent_path):                                        
    for child in d[parent]:                                                     
        child_path = f"{parent_path}/{child}"                                   
        print(child_path)                                                       
        print_children(child, child_path)                                       
                                                                                
print_children("parent", "parent")

输出:

parent/child1
parent/child1/child1_1
parent/child1/child1_1/child1_1_1
parent/child1/child1_1/child1_1_2
parent/child1/child1_2
parent/child2
parent/child2/child2_1
parent/child2/child2_2

正如 @OmidRoshani 所提到的,如果要将 child3 和 child4 包含在输出中,你需要将它们添加到父级中。

英文:

You can use a recursive function to do this.

d = {'parent':['child1','child2'], 'child1': ['child1_1','child1_2'], 'child2': ['child2_1','child2_2'], 'child3': [], 'child1_1': ['child1_1_1', 'child1_1_2'], 'child1_1_1': [], 'child1_1_2': [], 'child1_2': [], 'child2_1': [], 'child2_2': [], 'child4': []}
                                                                                
def print_children(parent, parent_path):                                        
    for child in d[parent]:                                                     
        child_path = f"{parent_path}/{child}"                                   
        print(child_path)                                                       
        print_children(child, child_path)                                       
                                                                                
print_children("parent", "parent")

Output:

parent/child1
parent/child1/child1_1
parent/child1/child1_1/child1_1_1
parent/child1/child1_1/child1_1_2
parent/child1/child1_2
parent/child2
parent/child2/child2_1
parent/child2/child2_2

As @OmidRoshani mentions, you need to add child3 and child4 to the parent if you want them to be included in the output.

huangapple
  • 本文由 发表于 2023年7月13日 16:24:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677336.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定