在Python中实现递归时遇到问题

时间:2014-02-13 21:50:17

标签: python recursion

我已经阅读了尽可能多的递归线程,但我仍然有点丢失。我不是在寻找任何人专门为我编写代码,但我会非常感谢某些方向。

我有一大块JSON,我已将其作为字典导入Python。我有一个功能,将查看记录并将ID添加到列表中。但是,此功能只会查看孩子的第一个“级别”。有人可以建议我如何开始递归地调用这个函数,这样它会跟着树一样深入它的多层次吗?

顺便说一句,我的数据基本上是一个二分树,所以我需要它遵循多个分支路径。

这是我目前非常混乱的功能:

def addFighter(fighterDict, database):
    childID = [child["id"] for child in fighterDict["children"]]
    database.append(childID)

    for child in childID:
        addFighter(child)

我知道我的for循环是错误的,但我不知道在哪里可以找到解释后续步骤的适当资源。

感谢您的帮助!

编辑:

是的,我的意思是二分树。对不起,如果这不是CS术语。我来自野生动物生物学背景。 :)

我的数据是巴西柔术黑带血统。例如,Mitsuyo Maeda训练Luis Franca和Carlos Gracie Sr. Luis Franca训练了Oswaldo Fadda,他训练了9个人。 Carlos Gracie Sr.培训了21人,依此类推。数据深度很多,包含1,664条黑带。

我已将数据从JSON转换为大型嵌套字典。我正在尝试将其转换为平面字典,以便可以使用Psycopg2将其导入Postgres。我的最终目标是在一列中列出一个战斗机,第二列是一个ID列表,或者是他们训练过的人的名字。

EDIT2:

代码片段:(我不确定这里格式化的最佳方式)

{"id":-301,"name":"The Beginning","data":"Mitsoyo Maeda, children [id]","children":    [{"id":467,"name":"Takeo Iano ","data":"Mitsoyo Maeda, children [id]","children":[{"id":974536,"name":"Francisco Sa ","data":"Mitsoyo Maeda, children [id]","children":[{"id":975533,"name":"Daniel Beleza ","data":"Mitsoyo Maeda, children [id]","children":[]},{"id":1168951,"name":"Carlos Pinto Sa ","data":"Mitsoyo Maeda, children [id]","children":[{"id":1169948,"name":"Reginaldo de Almeida ","data":"Mitsoyo Maeda, children [id]","children":[]},{"id":1272639,"name":"Ademir Oliveira ","data":"Mitsoyo Maeda, children [id]","children":[]},{"id":1273636,"name":"Daniel Taveras ","data":"Mitsoyo Maeda, children [id]","children":[]}]}]}]},{"id":1464,"name":"Mitsuyo Maeda ","data":"Mitsoyo Maeda, children [id]","children":[{"id":2461,"name":"Luis Franca ","data":"Mitsoyo Maeda, children [id]","children":[{"id":4455,"name":"Oswaldo Fadda ","data":"Mitsoyo Maeda, children [id]","children":[{"id":83218,"name":"Sergio Amarel ","data":"Mitsoyo Maeda, children [id]","children":[]},{"id":84215,"name":"Ronald Bauer ","data":"Mitsoyo Maeda, children [id]","children":[{"id":1230765,"name":"Victor da Costa ","data":"Mitsoyo Maeda, children [id]","children":[]}]},{"id":85212,"name":"Beto Ferrao ","data":"Mitsoyo Maeda, children [id]","children":[{"id":160984,"name":"Andre Assuncao ","data":"Mitsoyo Maeda, children [id]","children":[]}]},

1 个答案:

答案 0 :(得分:2)

看起来非常接近。根据我对情况的理解,这更合适:

database = []

def addFighter(fighterDict, database):
  # add this fighters id to the database
  database.append(fighterDict["id"])

  # get children if any
  children = fighterDict.get("children", [])

  for child in children:
    # for each child, run the addFighter method
    addFighter(child, database)

addFighter(someDictOfFighters, database)

print database

所以,看起来你有战斗机词典,至少包含id和子键,其值为更为战斗机的词典。如下所示:{“id”:1,“children”:[{“id”:2,“children”:[{“id”:4}]},{“id”:3}]}。我认为我做对了。

将此函数传递给函数,它获取第一个id(1)并将其添加到数据库中。然后它会看到孩子们(有ids 2和3)并在每个孩子身上运行这个功能。

对于id 2,它将2添加到数据库,然后在其子项上运行该函数(4)。

4没有子节点因此它向数据库添加4,为子节点获取一个空列表,并且不再运行任何递归。

然后我们一直回到函数的第一个实例,并在id为3的dict上调用它.3将被添加到数据库中,并且将为子节点和函数存在一个空列表不会再从那里打电话。

我们回到第一个电话,我们完成了for循环。我们知道,假设数据库是一个列表,数据库= [1,2,4,3]。