jeudi 22 novembre 2018

Python 3: applying recursion to compare lists?

I'm trying to define a function with the following that takes two parameters, list_1 and list_2, and compares them as follows: it returns False if list_1 and list_2 have different lengths, True if list_1 and list_2 are identical, and otherwise returns the index of the first mismatched elements between the lists.

So far I have this:

if len(list_1) != len(list_2):
    return False
elif list_1 == list_2:
    return True
else:
    i = 0
    if list_1[i] == list_2[i]:
        return i
    else:
        i += 1
        compare_lists(list_1, list_2)

Logically i keeps getting reset to 0 when I call the function again. Does anyone know how I can overcome this?

Aucun commentaire:

Enregistrer un commentaire