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:
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:
The second code:
The third code:
Why?
Thank you very much.
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 resultand 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 resultand 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 resultand result:
['1', '2', '#11', '#12', '##111', '##112', '#13', '3']It working!!!
Why?
Thank you very much.