拓扑排序、递归、使用生成器
数据:依赖列表,已验证为非循环。 所以在这里,“a”取决于“b”、“c”(c 取决于 d)等...
A = { 'a' : dict(b=1, c=1),
'c' : dict(d=1),
'd' : dict(e=1,f=1,g=1),
'h' : dict(j=1)
}
我想要一个自上而下的递归解决方案,比方说,找到从 'a': a, c, d, e, g, f, b
所以,现在(非生成器解决方案):
def get_all(D,k):
L = []
def get2(D,k):
L.append(k)
for ii in D.get(k,[]):
get2(D, ii)
get2(D,k)
return L
显然,这非常弱:)我一直在绞尽脑汁思考如何如何获得里面有收益,我很感激你们能带来的任何 py-foo 。
Data: a dependency list, already verified to be acyclic. So here, 'a' depends on 'b','c' (c depends on d), etc...
A = { 'a' : dict(b=1, c=1),
'c' : dict(d=1),
'd' : dict(e=1,f=1,g=1),
'h' : dict(j=1)
}
I'd like to have a top-down, recursive solution to let's say, find the chain starting at
'a': a, c, d, e, g, f, b
So, right now (a non-generator solution):
def get_all(D,k):
L = []
def get2(D,k):
L.append(k)
for ii in D.get(k,[]):
get2(D, ii)
get2(D,k)
return L
Obviously, this is pretty weak :) I've been banging my head about how to how to get yields inside there, and I'd appreciate any py-foo y'all can bring to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两个答案都给出相同的结果,但如果我对问题的阅读是正确的,则对给定图形的简单更改给出错误的答案 - 如果您从“b”添加对“c”的依赖(这不会引入循环)如图所示)输出为:
A
C
d
e
G
F
乙
d
e
G
F
这并不完全有帮助。 尝试这个小变化,它会跟踪图表中的哪些节点已经被访问过:
Both answers give the same result, but if my reading of the question is correct give the wrong answer to a simple alteration to the given graph - if you add a dependency on 'c' from 'b' (which doesn't introduce a cycle as the graph is directed) the output is:
a
c
d
e
g
f
b
d
e
g
f
which isn't totally helpful. Try this small variation, which keeps track of which nodes of the graph have already been visited:
试试这个:
给我
Try this:
Gives me