I am implementing the python code for this statement: Implement a function called cantAparicionesSub that take two integer lists as a parameter, and calculate the amount of elements of the first list that are also in the second. Also return a list with the common elements between the two lists.
For this I wrote this code:
def cantAparicionesSub(lista1, lista2):
lista_final = []
contador = 0
for i in range( len(lista1) ):
for j in range( len(lista2) ):
if lista1[i] == lista2[j]:
lista_final.append(i)
contador = contador + 1
print("Cantidad de elementos comunes: ", contador)
return contador, lista_final
Testing, for example with:
listaA = [1,1,1,1,1,1,2]
listaB= [1]
res = cantAparicionesSub(listaA, listaB)
print(res)
It results in 6 elements of the listA that are in B. But the list of common elements gives: [0, 1, 2, 3, 4, 5]. Always generate a list with the increase of i and I have not managed to obtain the expected list.