2
I have a list structure inside the list (list=[[ ],[ ]]). Example:
lista = [['pedro', 'marcos', 'mario', 'pedro', 'marcos', 'mario'],[5.2, 2.2, 6.1, 6.2, 1.1, 5.0]]
I am trying to remove the repeated elements from the first list based on the second most value list will be assigned to the new list. Output example:
lista_saida = [['pedro', 'marcos', 'mario'],[6.2, 2.2, 6.1]]
This is a draft that I did unsuccessfully:
lista = [['pedro', 'marcos', 'mario', 'pedro', 'marcos', 'mario'],[5.2, 2.2, 6.1, 6.2, 1.1, 5.0]]
lista_saida = [[],[]]
elemento = []
maior_indice = 0
for i in range(len(lista[0])):
for j in range(len(lista_saida[0])):
if lista[0][i] == lista_saida[0][j]:
x = lista[1][i]
y = lista_saida[1][j]
if x >= y:
maior_indice = lista[1][i]
elemento = lista[0][i]
else:
maior_indice = lista_saida[1][j]
elemento = lista_saida[0][j]
else:
maior_indice = lista[1][i]
elemento = lista[0][i]
lista_saida[0].append(elemento)
lista_saida[1].append(maior_indice)
Out of my code:
[[[], 'marcos', 'mario', 'pedro', 'marcos', 'mario'],[0, 2.2, 6.1, 6.2, 1.1, 5.0]]
Any help with logic is welcome.
Great answer, solved the proposed problem. I replicated your encoding and it worked perfectly in the example presented, however when I insert a longer list with 5000 elements the code give an error in the line of Elif: Indexerror: list index out of range, could help me?
– João
could pass me the test data?
– lmonferrari
John, I was comparing
nova_lista[1][lista[0].index(l)]
which would lead to error because I should compare it tonova_lista[1][nova_lista[0].index(l)]
. The way it was I couldn’t compare the values within the correct vector. I think it was kkkk sleep. I updated the code. Hug!– lmonferrari
"served as a glove" the code update. Thank you for making available of your time to help me. Hug!
– João