Python Forum

Full Version: Recursive Function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all ,
This is going to be crazy but I need to know how to do it. Please help me out folks.

The code which I'm using is not in a proper optimized way. Can anyone help me out in writing optimised code of it. (that is using recursive functions,etc..)

Here is my code:
NestedVariable is going to search from meta_data and it is type dict which I created already which act as input
         table1 = NestedVariable.text
            if "Expression" in table1:
                inner_selist = re.findall(r"Expression\('(.*?)'\)",table1.strip())
                if inner_selist:
                    inner.append({"Level0" : inner_selist})
                    for element in inner_selist:
                        for key,value in meta_data.items():
                            if key == element:
                                in_se = key
                                if "Expression" in value:
                                    inner_selist1 = re.findall(r"Expression\('(.*?)'\)",value.strip())
                                    if inner_selist1:
                                        inner.append({"Level1_": inner_selist1})
                                        for element in inner_selist1:
                                            for key1,value1 in meta_data.items():
                                                if key1 == element:
                                                    in_se1 = key1
                                                    if "Expression" in value1:
                                                        inner_selist2 = re.findall(r"Expression\('(.*?)'\)",value1.strip())
                                                        if inner_selist2:
                                                            inner.append({"Level2_" : inner_selist2})
                                                            for element in inner_selist2:
                                                                for key2,value2 in meta_data.items():
                                                                    if key2 == element:
                                                                        in_se2 = key2
                                                                        if "Expression" in value2:
                                                                            inner_selist3 = re.findall(r"Expression\('(.*?)'\)",value2.strip())
                                                                            if inner_selist3:
                                                                                inner.append({"Level3_" : inner_selist3})
                                                                                for element in inner_selist3:
                                                                                    for key3,value3 in meta_data.items():
                                                                                        if key3 == element:
                                                                                            in_se3 = key3
                                                                        else:
                                                                            level = 'level3'
                                                        
                                                    else:
                                                        level = 'level2'
                                    
                                else:
                                    level = 'level1'
                        
            else:
                level = 'level0'
Hope y'll understand my problem here.

Thanks in advance!
Your code looks a bit strange, with the indentation in line 2.
Paul
It's just my opinion but, an array, list, dict, or whatever should not ever go any deeper than two, three at most. You're just asking for problems going that deep in your structure. You should rethink how you're storing your information.
@DPaul I editted my code. Thanks.
(Jul-14-2020, 04:07 PM)menator01 Wrote: [ -> ]You're just asking for problems going that deep in your structure.

Actually, this isn't always true. Nested structures and recursion is a good fit for certain problems because there are efficient algorithms for searching and changing such structures (and the solutions to problems can be written quite elegantly). Examples of where it makes sense: search trees in games like chess or tic-tac-toe, modelling file systems, b-trees as used in databases and there are likely countless others.
Hi,

The indentation is still not OK, and without NestedVariable.txt
we have a very hard time figuring out what you are trying to do.
Is this a game of some kind ?

Paul
@ ndc85430 you may be right. From other languages I've always found that this was frowned upon. So I try not to use it.
You are using a dictionaries. There is no reason to loop through the keys to find matches. Instead of doing this:
           for element in inner_selist:
               for key,value in meta_data.items():
                   if key == element:
                       in_se = key
                       if "Expression" in value:
                           inner_selist1 = re.findall(r"Expression\('(.*?)'\)",value.strip())
                           if inner_selist1:
                               inner.append({"Level1_": inner_selist1})
Do this
           for element in inner_selist:
               if value := meta_data.get(element):
                   in_se = element
                   if "Expression" in value:
                       if inner_selist1 := re.findall(r"Expression\('(.*?)'\)",value.strip()):
                            inner.append({"Level1_": inner_selist1})
This does appear to be a good problem for a recursive solution, especially if you don't know the maximum depth of NestedVariable.depth. A detailed description of what this code does, what the input is and what you want for output is a good first step at defining what the recursive function will do. Once you have a description of what the recursive function does, writing the code is simple.