Recursion in Python

Asked

Viewed 132 times

0

I am trying to make 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. This is my code:

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

Someone might help me figure out what’s wrong here ?

  • 1

    Please explain what your code should do, not tell what is wrong if you don’t know what the purpose of the algorithm is. Tapping your eye up to assume it is a recursive counter of sublists within the original list, but it is assumption, edit your question and make it clearer.

1 answer

1

There’s too much wrong, come on:

  1. Apparently, you want to identify if a list contains sublists. On line 3, your if just exchange the boolean value and does not generate a condition, the return will always be 0(Zero)

  2. elif only searches the sublist in the first list item.

You better rethink your program.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.