Python Forum
Help with recursive functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with recursive functions
#1
Hi Friends,

I'm relatively new to Python recursive functions.
(Sorry my english.)

If expanding the Python list, I think they are the same:

list.append('#'), list += ['#'], list = list + ['#']

Or not?

My recursive tuple:

rec_tuple = (
    ("one", "1"),
    ("two", "2"),
    ("level-2", (
        ("eleven", "11"),
        ("twelve", "12"),
        ("level-3", (
            ("h_eleven", "111"),
            ("h_twelve", "112"),
           ),
         ),
        ("thirteen", "13"),
       ),
     ),
    ("three", "3"),
)
The function:
If this move up one level, I'll add a signal to the beginning, and this move down one level, I will remove it.

The code:

def rec_funct(value, level=[], result=None):
    if result is None:
        result = []
    for v in value:
        if isinstance(v[1], str):
            result.append("{}{}".format(''.join(level), v[1]))
        else:
            level.append('#')
            rec_funct(v[1], level, result)
            level = level[:-1]
    return result
and result:

>>>rec_funct(rec_tuple)
['1', '2', '#11', '#12', '##111', '##112', '#13', '#3']
It not working!!!

The second code:

def rec_funct(value, level=[], result=None):
    if result is None:
        result = []
    for v in value:
        if isinstance(v[1], str):
            result.append("{}{}".format(''.join(level), v[1]))
        else:
            level += ['#']
            rec_funct(v[1], level, result)
            level = level[:-1]
    return result
and result:
['1', '2', '#11', '#12', '##111', '##112', '#13', '#3']
It not working!!!


The third code:

def rec_funct(value, level=[], result=None):
    if result is None:
        result = []
    for v in value:
        if isinstance(v[1], str):
            result.append("{}{}".format(''.join(level), v[1]))
        else:
            level = level + ['#']
            rec_funct(v[1], level, result)
            level = level[:-1]
    return result
and result:

['1', '2', '#11', '#12', '##111', '##112', '#13', '3']
It working!!!

Why?

Thank you very much.
Reply
#2
Try to understand these simple examples which explain the issue
>>> def funcA(level):
...     level = level + ['#']
... 
>>> level = []
>>> funcA(level)
>>> level
[]
>>> def funcB(level):
...     level.append('#')
... 
>>> funcB(level)
>>> level
['#']
>>> level = []
>>> def funcC(level):
...     level.append('#')
...     level = level[-1]
... 
>>> funcC(level)
>>> level
['#']
>>> def funcD(level):
...     level.append('#')
...     del level[-1]
... 
>>> level = []
>>> funcD(level)
>>> level
[]
Reply
#3
I don't understand what the objective is, so no help here. However, I would like to remind what will happen if there is parameter level = []:

>>> def example(level=[]):
...     level.append(1)
...     return level
... 
>>> example()
[1]
>>> example()
[1, 1]
>>> example()
[1, 1, 1]
>>> def example_2(num, level=[]):
...     level.append(num)
...     return level
... 
>>> example_2(1)
[1]
>>> example_2(2)
[1, 2]
>>> example_2('3')
[1, 2, '3']
>>> def example_3(num, level=[]):
...     level.append(num)
...     return [num * num for num in level]
... 
>>> example_3(1)
[1]
>>> example_3(2)
[1, 4]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Combine Two Recursive Functions To Create One Recursive Selection Sort Function Jeremy7 12 7,198 Jan-17-2021, 03:02 AM
Last Post: Jeremy7
  Help in understanding scope of dictionaries and lists passed to recursive functions barles 2 3,148 Aug-11-2018, 06:45 PM
Last Post: barles

Forum Jump:

User Panel Messages

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