Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursive Function
#1
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!
Reply
#2
Your code looks a bit strange, with the indentation in line 2.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
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.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#4
@DPaul I editted my code. Thanks.
Reply
#5
(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.
Reply
#6
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#7
@ ndc85430 you may be right. From other languages I've always found that this was frowned upon. So I try not to use it.
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  with open context inside of a recursive function billykid999 1 574 May-23-2023, 02:37 AM
Last Post: deanhystad
  Why recursive function consumes more of processing time than loops? M83Linux 9 4,226 May-20-2021, 01:52 PM
Last Post: DeaD_EyE
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,358 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Execution of Another Recursive Function muzikman 5 3,002 Dec-04-2020, 08:13 PM
Last Post: snippsat
  Don't Understand Recursive Function muzikman 9 3,668 Dec-03-2020, 05:10 PM
Last Post: muzikman
  list call problem in generator function using iteration and recursive calls postta 1 1,895 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  Recursive function returns None, when True is expected akar 0 3,386 Sep-07-2020, 07:58 PM
Last Post: akar
  factorial using recursive function spalisetty06 12 4,053 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  Nested Recursive Function not Returning Etotheitau 2 2,256 May-09-2020, 06:09 PM
Last Post: Etotheitau
  Information "creeps up" in recursive function InigoMontoya 2 1,851 Sep-17-2019, 06:25 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020