繁体   English   中英

Python:从父子关系计算路径

[英]Python: Calculating the Path from Parent Child Relationships

父项)作为CSV(700,000行)输入

Child   Parent
fA00    f0
fA9 fA0
fA31    fA0
fA30    fA0
fA1 fA00
dccfA1  fA00
fA2 fA00
fA3 fA00
fA01    fA00
fA4 fA00
fA5 fA00
fA6 fA00
fA7 fA00
fA0 fA00
fA142149    fA00
fA02    fA00
fA8 fA00
qA1 fA10
fA22    fA10
fA23    fA10
fA11    fA10
qA2     fA10
fA15    fA11
fA13    fA11
fA12    fA11
fA14    fA13
fA17    fA16
fA18    fA17
fA19    fA17
fA20    fA17
fA21    fA19
etc....

它上升到14个深度。 父级最高的是f0

我想遍历子父母关系以确定路径

预期结果

f0 --- top
f0\fa00
f0\fa00\.Child
f0\fa00\.Child2etc
f0\fA0
f0\fA0\.Child
f0\fA0\.Child2etc

如何在Python中执行此操作?

我开始思考复杂的树结构递归构造,但是基本上它很简单。 创建子项到父项的映射,然后从子项列表开始,列出其父项,然后从父项的父项到顶部。 递归例程很容易提取孩子的祖先。

'''
This is the family tree:
------------------------
f0:
    a0:
        b0
        b1:
        b2:
    a1:
        b3:
        b4:
    a2:
        b5:
            c0
            c1
'''
ancestry = [
    ('b1', 'a0'),
    ('c1', 'b5'),
    ('b2', 'a0'),
    ('b3', 'a1'),
    ('b4', 'a1'),
    ('b5', 'a2'),
    ('a0', 'f0'),
    ('a1', 'f0'),
    ('a2', 'f0'),
    ('b0', 'a0'),
    ('c0', 'b5'),
]

代码是:

parents = set()
children = {}
for c,p in ancestry:
    parents.add(p)
    children[c] = p

# recursively determine parents until child has no parent
def ancestors(p):
    return (ancestors(children[p]) if p in children else []) + [p]

# for each child that has no children print the geneology
for k in (set(children.keys()) - parents):
    print '/'.join(ancestors(k))

输出为:

f0/a1/b4
f0/a0/b0
f0/a0/b1
f0/a0/b2
f0/a1/b3
f0/a2/b5/c1
f0/a2/b5/c0

我将其作为练习来阅读csv文件 ,并可能对输出进行更好的排序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM