0

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.

3
  • Your question is not clear, because you don't show what x is when you call recursion(x, 4) and you don't specify the expected output for this case Commented Apr 27, 2016 at 15:48
  • Do you expect to get [[[['s'], 'p'], 'a'], 'm'] or: '[[[['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm']] Commented Apr 27, 2016 at 15:50
  • Thanks first,it's my wrong that I don't show your what I want to get.In fact,the result I want is '[[[['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm'], ['s', 'p', 'a', 'm']] ,thanks for your attention again. Commented Apr 28, 2016 at 13:16

3 Answers 3

2

I'm also not sure what you want to happen, but if you want to split a string into its characters and put them into nested lists, this function works.

def recursion(l,s):
    if s == "":
        return l;
    elif len(l) == 0:
        nL = [s[0]]
        return recursion(nL,s[1:])
    else:
        nL = [l,s[0]]
        return recursion(nL,s[1:])

So for example

print recursion([],"spam")

would output

[[[['s'], 'p'], 'a'], 'm']
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not sure what you want to happen, but here's what is happening:

def recursion(x,i):
   x.append(list('spam'))
   x=[x]

Here, x becomes [['s','p','a','m']]. But it doesn't change after this. When you call recursion(x,i) a few lines later, this does not affect the original value of x.

Maybe if you do x = recursion(x,i) instead it will give you what you want, because x will actually be change at the top level.

1 Comment

Thank you so much,I make me know why it happens.But you must miss something ,I have updated my post for the x value I want.And I try to use your code to settle my problem ,I failed.May I ask for a working codes?Thank you again.
0

I think you might be confused by the fact that a Python string ("spam" is a string) is in many ways equivalent to a Python list. You can index them, get their len(), etc. In particular, pretty much anything you do in square brackets works for both string and list types.

Your first example is of a single-byte string, [[['a'], 'a'], 'a'] but you don't give us some key details: what was the input that you expected to produce this output?

For example, the input might have been:

func('a', 3)

Or might have been:

func('aaa')

Or might have been:

func(['a'], 3)   # or ,2)

Or might have been:

func(['a', 'a', 'a'])

Any of those would be a reasonable starting point for a return value of [[['a'], 'a'], 'a'].

Solution

Because your example function takes a second parameter, i, I'm going to assume there is a real need for it. So let's go with the simplest possible case:

def func(lst, times):
    """
    Given a list and a multiplier > 0, return a "nested" list with 
    the contents of the original list repeated (nested) that many
    times:

        Input:  [a,b],2
        Output: [[a,b],a,b]
    """
    assert times > 0
    if times == 1:
        return lst[:]
    else:
        result = lst[:]
        result.insert(0, func(lst, times-1))
        return result

    for times in range(1,4):
        print(func(['a', 'b'], times))

Alternate

Here's a simpler function, that doesn't assume a list:

def func(content, times):
    """
    Given content and a multiplier > 0, return a set of nested lists,
    with the requested depth, each containing optionally any further
    nested lists and the given content.

        Input:  'content', 2
        Output: [['content'], 'content']
    """
    assert(times > 0)
    if times == 1:
        result = [content]
    else:
        result = [func(content, times-1)]
        result.append(content)
    return result

for times in range(1,4):
    print(func('a', times))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.