I've read as many recursion threads as possible but I'm still kind of lost. I'm not looking for anyone to specifically code this for me but I would greatly appreciate some direction.
I have a big block of JSON that I have imported into Python as a dictionary. I have a function that will look through the records and add the IDs to a list. However, this function will only look at the first "level" of children. Can someone suggest how I begin to call this function recursively so it will follow the tree for however many levels deep it goes?
BTW, my data is essentially a dichotomous tree so I need it to follow multiple branching paths.
Here is the very messy function I have currently:
def addFighter(fighterDict, database):
childID = [child["id"] for child in fighterDict["children"]]
database.append(childID)
for child in childID:
addFighter(child)
I know my for loop is wrong, but I'm not sure where to find the proper resources that will explain the next steps.
Thank you for your help!
Edit:
Yes, I meant dichotomous tree. Sorry if this is not a CS term. I come from a wildlife biology background. :)
My data is a Brazilian jiujitsu black belt lineage. For example, Mitsuyo Maeda trained Luis Franca and Carlos Gracie Sr. Luis Franca trained Oswaldo Fadda, who trained 9 people. Carlos Gracie Sr. trained 21 people and so on. The data is many levels deep and contains 1,664 black belts.
I have converted my data from JSON to a big nested dictionary. I'm trying to convert it to a flat dictionary so it can be imported into Postgres using Psycopg2. My end goal is to have a table that lists a fighter in one column and the second column would be a list of IDs or named of the people they trained.
Edit2:
Snippet of code: (I'm not sure of the best way to format this here)
{"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":[]}]},
TypeErrorwhen callingaddFighterwith only one argument. Or did you copy-paste your code incorrectly?