Python recursive loop function

I have a database that models the relationship of folders to n levels of nesting. For any given folder, I want to generate a list of all the child folders.

Assuming I have a function called "getChildFolders ()", which is the most efficient way to call such a recursive loop. The following code works for 4 levels of nesting, but I would like more flexibility in determining the depth of the recursion or in stopping the loop intelligently when there are no more children.

  folder_ids = []
  folder_ids.append( folder.id )
  for entry in child_folders:
    folder_ids.append( entry.id )
    child_folders_1 = getChildFolders(entry.id)
    for entry_1 in child_folders_1:
      folder_ids.append( entry_1.id )
      child_folders_2 = getChildFolders(entry_1.id)
      for entry_2 in child_folders_2:
        folder_ids.append( entry_2.id )
        child_folders_3 = getChildFolders(entry_2.id)
        for entry_3 in child_folders_3:
          folder_ids.append( entry_3.id )
+3
source share
5 answers

A recursive function is a good way to do this:

def collect_folders(start, depth=-1)
    """ negative depths means unlimited recursion """
    folder_ids = []

    # recursive function that collects all the ids in `acc`
    def recurse(current, depth):
        folder_ids.append(current.id)
        if depth != 0:
            for folder in getChildFolders(current.id):
                # recursive call for each subfolder
                recurse(folder, depth-1)

    recurse(start, depth) # starts the recursion
    return folder_ids
+2
source

, python, - .

def collect_folders(start):
    stack = [start.id]
    folder_ids = []
    while stack:
        cur_id = stack.pop()
        folder_ids.append(cur_id)
        stack.extend(folder.id for folder in getChildFolders(cur_id))
    return folder_ids

, getChildFolders , . - , , , .

+3
def my_recursive_function(x, y, depth=0, MAX_DEPTH=20):
    if depth > MAX_DEPTH:
        return exhausted()
    elif something(x):
        my_recursive_function(frob(x), frob(y), depth + 1)
    elif query(y):
        my_recursive_function(mangle(x), munge(y), depth + 1)
    else:
        process(x, y)

# A normal call looks like this.
my_recursive_function(a, b)

# If you're in a hurry,
my_recursive_function(a, b, MAX_DEPTH=5)
# Or have a lot of time,
my_recursive_function(a, b, MAX_DEPTH=1e9)
+2
source

This is closest to your code and very unsportsmanlike:

def recurse(folder_ids, count):
  folder_ids.append(folder.id)
  for entry in child_folders:
    folder_ids.append(entry.id)
    child_folders_1 = getChildFolders(entry.id)
    if count > 0:
        recurse(folder_ids, count-1)

folder_ids = []
recurse(folder_ids, 4)

You should probably look for os.walkand use a similar approach to iterate over the tree.

0
source

I need something like this to check the hierarchical tree. You can try:

def get_children_folders(self,mother_folder):
    '''
    For a given mother folder, returns all children, grand children
    (and so on) folders of this mother folder.
    '''
    folders_list=[]
    folders_list.append(mother_folder)
    for folder in folders_list:
        if folder not in folders_list: folders_list.append(folder)
        new_children = getChildFolders(folder.id)
        for child in new_children:
            if child not in folders_list: folders_list.append(child)
    return folders_list
0
source

Source: https://habr.com/ru/post/1773953/


All Articles