Remove Repeated Elements from a List[0] from the List Database[1]

Asked

Viewed 83 times

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.

1 answer

2


lista = [['pedro', 'marcos', 'mario', 'pedro', 'marcos', 'mario'],[5.2, 2.2, 6.1, 6.2, 1.1, 5.0]]
nova_lista = [[],[]]

for l, j in zip(lista[0], lista[1]):
    
        if l not in nova_lista[0]: 
            nova_lista[0].append(l)
            nova_lista[1].append(j)
        if l in nova_lista[0] and nova_lista[1][nova_lista[0].index(l)] < j:
            nova_lista[1][nova_lista[0].index(l)] = j

Exit:

print(nova_lista)
[['pedro', 'marcos', 'mario'], [6.2, 2.2, 6.1]]

There are several possibilities to solve, this is one of them.

In the first if we check if the items do not yet exist in the new list, if there is not added the item. In Elif we check if the item already exists within the new list, if it exists, we check if the value float is greater than what already existed, if positive we change, if negative we move to the next iteration.

  • 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?

  • could pass me the test data?

  • John, I was comparing nova_lista[1][lista[0].index(l)] which would lead to error because I should compare it to nova_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!

  • 1

    "served as a glove" the code update. Thank you for making available of your time to help me. Hug!

Browser other questions tagged

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