丢给你一段代码
class Node(object): def __init__(self, name): self.parents =[] self.nexts = [] self.name=name def build(): _list=[(1,2),(3,5),(3,9),(8,6),(3,8),(5,10),(2,10),(2,3),(8,10),(8,3)] _map={} for n in _list: if n[0] in _map: n1=_map[n[0]] else: n1=Node(n[0]) if n[1] in _map: n2 = _map[n[1]] else: n2 = Node(n[1]) n2.parents.append(n1) n1.nexts.append(n2) _map[n[0]]=n1 _map[n[1]] = n2 return _map _map=build(); def join_path(k): _lists=[] def path(_map,k,_list): list1=_list.copy() list1.append(_map[k]) if _map[k] in set(_list) or len(_map[k].nexts)==0: _lists.append(list1) else: for n in _map[k].nexts: path(_map,n.name,list1) path(_map,k,[]) return _lists # return _list ######## for k in (1,2,3): _lists=join_path(k) print('join '+str(k)+'_________') for ls in _lists: ns=[] for l in ls: ns.append(l.name) print(ns)