Recursion in Python

0

I'm trying to do a recursive function in Python that is not assigning me the correct results and when I try to change it it always happens stack overflow. My code is this:

def sublistas(lista):
i=0
if not isinstance(lista,list):
    return 0
elif type(lista[i])==list:
    return sublistas(lista[i])+(1)
else:
    return 0

Is anyone able to help me figure out what's wrong here?

    
asked by anonymous 06.01.2016 / 23:16

1 answer

1

There's a lot wrong, come on:

  • Apparently, you want to identify if a list contains sublists. In line 3, its if only changes the Boolean value and does not generate a condition, the return will always be 0 (Zero)

  • elif only searches the sublist on the first item in the list.

  • You'd better rethink your program.

        
    07.01.2016 / 11:51