xpath语法 - python爬虫lxml




Python:父子层次的组合 (2)

你看过这篇精彩的文章吗? 真正理解Python中的模式是必不可少的阅读。 你的问题可以被认为是一个图形问题 - 找到关系基本上是找到从子节点到父节点的所有路径。

由于可以有任意数量的嵌套(child-> parent1-> parent2 ...),因此需要递归解决方案来查找所有路径。 在你的代码中,你有2个for循环 - 当你发现的时候,最多只能得到3个级别的路径。

下面的代码是根据上面的链接修改你的问题。 函数find_all_paths需要一个图形作为输入。

让我们从您的文件中创建图表:

graph = {} # Graph is a dictionary to hold our child-parent relationships.
with open('testing.csv','r') as f:
    for row in f:
        child, parent = row.split(',')
        graph.setdefault(parent, []).append(child)

print graph

与您的示例,这应该打印:

{'C': ['A', 'B'], 'B': ['A'], 'D': ['B', 'C']}

以下代码是从作文中直接获得的:

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

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

    paths = []

    for node in graph[start]:
        if node not in path:
            newpaths = find_all_paths(graph, node, end, path)
            for newpath in newpaths:
                paths.append(newpath)
    return paths

for path in find_all_paths(graph, 'D', 'A'):
    print '|'.join(path)

输出:

D|B|A
D|C|A
D|C|B|A

对于父子关系表(csv),我尝试使用表中的所有数据收集可能的父子关系组合链。 我试图在存在多个子父项的情况下(请参阅第3和4行),第二个子父组合(第4行)不包含在迭代中。

数据示例:

孩子,父母

A,B
A,C
B,D
B,C
C,D

预期的连锁业绩:

D|B|A
D|C|B|A
D|C|A

实际连锁结果:

D|B|A
D|C|A

find= 'A' #The child for which the code should find all possible parent relationships
sequence = ''
with open('testing.csv','r') as f:     #testing.csv = child,parent table (above example)
    for row in f:
        if row.strip().startswith(find):
            parent = row.strip().split(',')[1]
            sequence = parent + '|' + find
            f1 = open('testing.csv','r')
            for row in f1:
                if row.strip().startswith(parent):
                    parent2 = row.strip().split(',')[1]
                    sequence = parent2 + '|' + sequence
                    parent = parent2
        else:
            continue
        print sequence

我不确定这是否是最有效的方法(但是在每一行读取文件会更糟糕)。

find= 'A' #The child for which the code should find all possible parent relationships
sequences = set(find)

# we'll build up a chain for every relationship, then strip out un-needed ones later
with open('testing.csv','r') as f:     #testing.csv = child,parent table (above example)
    for row in f:
        child, parent = row.strip().split(',')
        sequences.add(parent + '|' + child)
        for c in sequences.copy():  
            if c[0] == child:
                sequences.add(parent + '|' + c)


# remove any that don't end with our child:
sequences = set(s for s in sequences if s.endswith(find))

# get all shorter chains when we have a longer one
extra = set()
for g1 in sequences:
    for g2 in sequences:
        if g2[2:] == g1:
            extra.add(g1)

# remove the shorter chains
sequences.difference_update(extra)

for chain in sequences:
    print(chain)

结果:

D|C|A
D|C|B|A
D|B|A




hierarchy