如何获取所有组合节点(给定字典和列表)并按 Python 中最长的节点排序

How to Get All Combination Node (Given dictionary and list) and sort by the longest node in Python

如何从 python 中的给定字典中获取所有组合(列出)?

我的字典输入法:

node_data = {
    "1":["2","3","4","5"],#1
    "2":["7","8"],#2
    "3":["6"],#3
    "4":[],#4
    "5":[],#5
    "6":["11"],#6
    "7":[],#7
    "8":["9","10",],#8
    "9":["12"],#9
    "10":[],#10
    "11":["13"],#11
    "12":[],#12
    "13":["14"],#13
    "14":[]#14   
}

期望输出(按最长节点排序):

["1","3","6","11","13","14"]
["1","2","8","9","12"]
["1","2","8","10"]
["1","2","7"]
["1","4"]
["1","5"]

我做了类似的事情,它似乎有效:

def recurse(current, nodes, path, all_path):
    path.append(current)
    if nodes[current]:
        for child in nodes[current]:
            recurse(child, nodes, path.copy(), all_path)
    else:
        all_path.append(path)
    return all_path
    
if __name__ == '__main__':
    node_data = {
        "1":["2","3","4","5"],#1
        "2":["7","8"],#2
        "3":["6"],#3
        "4":[],#4
        "5":[],#5
        "6":["11"],#6
        "7":[],#7
        "8":["9","10",],#8
        "9":["12"],#9
        "10":[],#10
        "11":["13"],#11
        "12":[],#12
        "13":["14"],#13
        "14":[]#14   
    }
    toto = recurse("1", node_data, [], [])
    toto.sort(key=len, reverse=True)
    print(toto)

希望对您有所帮助