I want to get a nesting list using python,looking likes
[[['a'],'a'],'a']
So,I wrote a recursion function to get it.
def recursion(x,i):
x.append(list('spam'))
x=[x]
i-=1
print('i value is %d'%i)
print(x)
if i>0:
print('start new recursion!')
recursion(x,i)
print('callback x"s value:',x)
#return x
But ,if I call this function like
x=[]
recursion(x,4)
The result of x is
[['s', 'p', 'a', 'm']]
I don't understand it,and I found that this function seem had get the right value of x through the stdout,
i value is 3
[[['s', 'p', 'a', 'm']]]
start new recursion!
i value is 2
[[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]]
start new recursion!
i value is 1
[[[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]]
start new recursion!
i value is 0
[[[[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]]
callback x"s value: [[[[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]]
callback x"s value: [[[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]
callback x"s value: [[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]
callback x"s value: [[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]
Please tell me what happen to x and why the function don't return x's value I wanted.Thanks so much,and apologize for my poor english.
#Thanks for your all attention.The value of x I want to get is
[[[[[['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']], ['s', 'p', 'a', 'm']]]
I'm sorry that I missed it in the first post.
recursion(x, 4)and you don't specify the expected output for this case[[[['s'], 'p'], 'a'], 'm']or:'[[[['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm']]