• <bdo id='Q8l08'></bdo><ul id='Q8l08'></ul>
        <tfoot id='Q8l08'></tfoot>

      1. <small id='Q8l08'></small><noframes id='Q8l08'>

      2. <legend id='Q8l08'><style id='Q8l08'><dir id='Q8l08'><q id='Q8l08'></q></dir></style></legend>
        <i id='Q8l08'><tr id='Q8l08'><dt id='Q8l08'><q id='Q8l08'><span id='Q8l08'><b id='Q8l08'><form id='Q8l08'><ins id='Q8l08'></ins><ul id='Q8l08'></ul><sub id='Q8l08'></sub></form><legend id='Q8l08'></legend><bdo id='Q8l08'><pre id='Q8l08'><center id='Q8l08'></center></pre></bdo></b><th id='Q8l08'></th></span></q></dt></tr></i><div id='Q8l08'><tfoot id='Q8l08'></tfoot><dl id='Q8l08'><fieldset id='Q8l08'></fieldset></dl></div>

        Python:父子层次结构的组合

        Python: Combinations of parent-child hierarchy(Python:父子层次结构的组合)

          <bdo id='xRQPX'></bdo><ul id='xRQPX'></ul>

          <small id='xRQPX'></small><noframes id='xRQPX'>

          <legend id='xRQPX'><style id='xRQPX'><dir id='xRQPX'><q id='xRQPX'></q></dir></style></legend>

            <i id='xRQPX'><tr id='xRQPX'><dt id='xRQPX'><q id='xRQPX'><span id='xRQPX'><b id='xRQPX'><form id='xRQPX'><ins id='xRQPX'></ins><ul id='xRQPX'></ul><sub id='xRQPX'></sub></form><legend id='xRQPX'></legend><bdo id='xRQPX'><pre id='xRQPX'><center id='xRQPX'></center></pre></bdo></b><th id='xRQPX'></th></span></q></dt></tr></i><div id='xRQPX'><tfoot id='xRQPX'></tfoot><dl id='xRQPX'><fieldset id='xRQPX'></fieldset></dl></div>
            <tfoot id='xRQPX'></tfoot>
                  <tbody id='xRQPX'></tbody>

                  本文介绍了Python:父子层次结构的组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  对于子父关系表 (csv),我正在尝试使用表中的所有数据收集可能的父子关系组合链.我正在尝试解决一个问题,即如果存在多个子父项(参见第 3 行和第 4 行),则迭代中不包含第二个子父项组合(第 4 行).

                  For a child-parent relationship table (csv), I am trying to gather possible parent to child relationship combination chains using all data in the table. I am trying against a problem where if multiple sub-parents exist (see rows 3 & 4), the second sub-parent combination (row 4) is not included in the iteration.

                  数据示例:

                  孩子,父母

                  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
                  

                  推荐答案

                  你看过 this 精彩的文章?真正理解python中的模式是必不可少的阅读.您的问题可以被认为是图问题 - 查找关系基本上是查找从子节点到父节点的所有路径.

                  Have you looked at this fantastic essay? It is essential reading to really understand patterns in python. Your problem can be thought of as a graph problem - finding the relationships is basically finding all paths from a child node to the parent node.

                  由于可能存在任意数量的嵌套(child->parent1->parent2...),因此您需要一个递归解决方案来查找所有路径.在您的代码中,您有 2 个 for 循环 - 正如您发现的那样,最多只会产生 3 级路径.

                  Since there could be an arbitrary amount of nesting (child->parent1->parent2...), you need a recursive solution to find all paths. In your code, you have 2 for loops - which will only result in 3level paths at most as you found out.

                  下面的代码改编自上面的链接,以解决您的问题.find_all_paths 函数需要一个图形作为输入.

                  The code below was adapted from the link above to fix your issue. The function find_all_paths requires a graph as an input.

                  让我们根据您的文件创建图表:

                  Let's create the graph from your file:

                  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
                  

                  使用您的示例,应打印:

                  with your sample, this should print:

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

                  以下代码直接来自论文:

                  The following code is straight from the essay:

                  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
                  

                  这篇关于Python:父子层次结构的组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Running .jl file from R or Python(从 R 或 Python 运行 .jl 文件)
                  Running Julia .jl file in python(在 python 中运行 Julia .jl 文件)
                  Using PIP in a Azure WebApp(在 Azure WebApp 中使用 PIP)
                  How to run python3.7 based flask web api on azure(如何在 azure 上运行基于 python3.7 的烧瓶 web api)
                  Azure Python Web App Internal Server Error(Azure Python Web 应用程序内部服务器错误)
                  Run python dlib library on azure app service(在 azure app 服务上运行 python dlib 库)

                      <small id='0NIEc'></small><noframes id='0NIEc'>

                          <bdo id='0NIEc'></bdo><ul id='0NIEc'></ul>
                          <i id='0NIEc'><tr id='0NIEc'><dt id='0NIEc'><q id='0NIEc'><span id='0NIEc'><b id='0NIEc'><form id='0NIEc'><ins id='0NIEc'></ins><ul id='0NIEc'></ul><sub id='0NIEc'></sub></form><legend id='0NIEc'></legend><bdo id='0NIEc'><pre id='0NIEc'><center id='0NIEc'></center></pre></bdo></b><th id='0NIEc'></th></span></q></dt></tr></i><div id='0NIEc'><tfoot id='0NIEc'></tfoot><dl id='0NIEc'><fieldset id='0NIEc'></fieldset></dl></div>
                        • <tfoot id='0NIEc'></tfoot>
                            <tbody id='0NIEc'></tbody>
                          • <legend id='0NIEc'><style id='0NIEc'><dir id='0NIEc'><q id='0NIEc'></q></dir></style></legend>