Python列表递归增加了额外的嵌套

Python列表递归增加了额外的嵌套,python,recursion,nested-lists,Python,Recursion,Nested Lists,我试图解决一个与这里列出的问题类似的问题: 期望输出: [['top', '1a', '2a', '3a'], ['top', '1a1'], ['top', '1b', '2b'], ['top', '1c', '2c', '3c', '4c']] 实际产量: [[[['top', '1a', '2a', '3a']]], ['top', '1a1'], [['top', '1b', '2b']], [[[['top', '1c', '2c', '3c', '4c']]]]]

我试图解决一个与这里列出的问题类似的问题:

期望输出:

[['top', '1a', '2a', '3a'],
 ['top', '1a1'],
 ['top', '1b', '2b'],
 ['top', '1c', '2c', '3c', '4c']]
实际产量:

[[[['top', '1a', '2a', '3a']]],
 ['top', '1a1'],
 [['top', '1b', '2b']],
 [[[['top', '1c', '2c', '3c', '4c']]]]]

有没有关于如何移除多余嵌套的建议?谢谢

以下内容可以解决您的问题:

 def find_all_paths(graph, start, path=None):
    if path is None: 
        # best practice, avoid using [] or {} as
        # default parameters as @TigerhawkT3 
        # pointed out.
        path = []
    path = path + [start]

    if not graph.has_key(start):
        return [path] # return the path as a list of lists!

    paths = []
    for node in graph[start]:
        # And now use `extend` to make sure your response
        # also ends up as a list of lists
        paths.extend(find_all_paths(graph, node, path))

    return paths

问题在于将单一列表的路径与列表的路径混淆。函数可以返回任意一个,具体取决于您在图形中的位置

您可能希望返回所有情况下的路径列表。因此,将基本情况下的
返回路径
更改为
返回[path]

在递归情况下,现在需要处理将每个子级的路径合并在一起的问题。我建议使用
路径。扩展(…)
而不是
路径。追加(…)

综上所述,你会得到:

def find_all_paths(graph, start, path=[]):
    path = path + [start]

    if not graph.has_key(start):
        return [path]

    paths = []

    for node in graph[start]:
        paths.extend(find_all_paths(graph, node, path))

    return paths

这是一个非递归的解决方案。然而,它通过对输出列表进行排序来“作弊”

def find_all_paths(edges):
    graph = {}
    for u, v in edges:
        if u in graph:
            graph[v] = graph[u] + [v]
            del graph[u]
        else:
            graph[v] = [u, v]
    return sorted(graph.values())

data = (
    ('top','1a'),
    ('top','1a1'),
    ('top','1b'),
    ('top','1c'),
    ('1a','2a'),
    ('1b','2b'),
    ('1c','2c'),
    ('2a','3a'),
    ('2c','3c'),
    ('3c','4c'),
)

test = find_all_paths(data)
for row in test:
    print(row)
输出

['top', '1a', '2a', '3a']                                                                                                                      
['top', '1a1']                                                                                                                                 
['top', '1b', '2b']                                                                                                                            
['top', '1c', '2c', '3c', '4c']

@这不是同一个问题!即使您将:
test=find_all_paths(graph,'top')
修改为
test=find_all_paths(graph,'top',[])
您也会遇到同样的问题,这与默认参数无关,因为由于第一次赋值,该参数不会在函数的范围内发生变化。我投票重新开始这个问题。是的,我会重新开始。我认为TigerhawkT3在这里有点鲁莽。“这个问题被标记为重复,但我相信这个问题与递归步骤有关,而不是与默认参数有关。如果我错了,请纠正我。”-不,你是对的。您的问题被标记为不正确的复制目标。请停止使用
has\u key
,它已被弃用。这比我提供的解释要好得多+1@Andrew:哎呀,是的,那是个打字错误。我已经编辑过了。很抱歉。我想这是一个很好的例子,说明了使用名称非常相似的变量的危险性。
['top', '1a', '2a', '3a']                                                                                                                      
['top', '1a1']                                                                                                                                 
['top', '1b', '2b']                                                                                                                            
['top', '1c', '2c', '3c', '4c']