Remove duplicates from a list keeping order

Asked

Viewed 1,198 times

1

I had the list:

A = [12,   15,  10,  15,  12,  10,  10,  10,  15,  12,  12,  15,  15,  15]

, in making:

ASemRepetidos=set(A)

get ([10, 12, 15]), as it turns out, whether in A my first element is 12, in set(A) becomes 10, ie I lose the original order of the elements...

If:

B=[0.2, 0.3, 1.1, 0.2, 0.2, 0.7, 0.4, 0.6, 0.1, 0.3, 0.7, 0.4, 0.5, 0.5]

By making this code:

indice=[]
feitos = [] # vou guardar aqui todas as chaves que já ocorreram no loop.
indic = {} # Se quiseres ter uma ligação directa entre chave e indice...
for k in A:
    if k in ASemRepetidos and k not in feitos: 
        indice.append(A.index(k))
        feitos.append(k)
        indic[k]=A.index(k)
obtenho os indices da 1ª ocorrencia de cada valor repetido e portanto posso fazer:
Re=[]
for g in indice:
    Re.append(B[g])

getting from the list B the values of the first repeating index. But if instead of wanting only the value of the 1st repeated index you want the sum of each repeated set, but of all sets, and maintaining the order of occurrence of the key of these sets in A and not of set(A) how do I do?

I already had this dictionary:

ASemRepetidos = set(A)

def indicesDeElementoNaLista(elementoProcurado, lista):
    return [i for (i, elemento) in enumerate(lista) if elemento == elementoProcurado]

def elementosNasPosicoes(lista, posicoes):
    return [lista[i] for i in posicoes]

dicionarioResultante = {} 

for elemento in ASemRepetidos:
    posicoes = indicesDeElementoNaLista(elemento, A)
    elementosCorrespondentes = elementosNasPosicoes(B, posicoes)
    dicionarioResultante[elemento] = elementosCorrespondentes
print(dicionarioResultante)

e para a soma de cada conjunto fazia:
A = [12,   15,  10,  15,  12,  10,  10,  10,  15,  12,  12,  15,  15,  15]
print set(A)
B = [0.2, 0.3, 1.1, 0.2, 0.2, 0.7, 0.4, 0.6, 0.1, 0.3, 0.7, 0.4, 0.5, 0.5]
B_lin = {} # coloque um nome mais adequado ao seu contexto

for elemento in set(A):
    posicoes = indicesDeElementoNaLista(elemento, A)
    elementosCorrespondentes = elementosNasPosicoes(B, posicoes)
    B_lin[elemento] = elementosCorrespondentes
B_Total=[]
for chave in set(A):
    B_Total.append(sum(B_lin[chave]))
    print chave, B_lin[chave]
#print B_Total

and got:

set([0, 1088, 1602, 1089, 9999, 1107, 1615, 1616, 1010, 1011, 1108, 1015, 1114, 1115])
set([10, 12, 15])
10 [1.1, 0.7, 0.4, 0.6]
12 [0.2, 0.2, 0.3, 0.7]
15 [0.3, 0.2, 0.1, 0.4, 0.5, 0.5]

that is, the set orders the 10-12-15, while I needed order to be maintained: 12-15-10, because after the search results of the elements by index in the list are not consistent with those presented in this function of the sum and the idea is the values to be in the end passed to an Excel by the way in which they arise initially.

If instead of the sum I want the minimum of each set:

Ili = []
listas_min = []
ind=[]
for i in range(len(indice)):
    valor = A[indice[i]]
    listas_min.append([])
    for j in range(len(A)):
        if A[j]==valor:
            listas_min[i].append(B[j])
            ind.append(j)
    Ili.append(min(listas_min[i]))
#print(Ili)
print ind

and it works, gives me the minimum value for each set. I also tried to return the respective index of the original list (A) and not in ASemRepetidos in which it happens, through .index but I couldn’t.

  • The minimum of each set does not have a respective index but the set of indices. Is this what you want? If yes save j in loop when A[i]=value.

  • Where exactly do I keep the code? It’s not giving...

  • Next to lists[i]. append(B[j]). Make another list like "lists" to store the indices.

  • I had to make a new change to the code because I wasn’t exactly calculating the minimums, now I am, but if I do what you say, I can’t.. gives me all the indexes instead of giving the minimum value of each set...

  • I thought that’s what you wanted. I edited the end of the answer with code to get the Dice out of the minimum of each set.

1 answer

1


If I understood the problem well then the following solution should solve the problem:

somas = []
listas = []
for i in range(len(indice)):
    valor = A[indice[i]]
    soma = 0
    listas.append([])
    for j in range(len(A)):
        if A[j]==valor:
            soma = soma + B[j]
            listas[i].append(B[j])
    somas.append(soma)
print(indice)                  # Indice correcto
print([A[i] for i in indice])  # Valores de A nos indices correctos
print(somas)                   # Somas por cada indice correcto
print(listas)                  # listas por cada indice correcto (que foram somadas)

, the result is:

[0, 1, 2]
[12, 15, 10]
[1.4, 2.0, 2.8000000000000003]
[[0.2, 0.2, 0.3, 0.7], [0.3, 0.2, 0.1, 0.4, 0.5, 0.5], [1.1, 0.7, 0.4, 0.6]]

, and this code was run right after you find the values of the first repeat input:

indice=[]
feitos = [] # vou guardar aqui todas as chaves que já ocorreram no loop.
indic = {} # Se quiseres ter uma ligação directa entre chave e indice...
for k in A:
    if k in ASemRepetidos and k not in feitos: 
        indice.append(A.index(k))
        feitos.append(k)
        indic[k]=A.index(k)

EDITED:

To have the minimums and indices of minimums:

minimos = []
indminimos = []
for i in listas:
    minimos.append(min(i))
    indminimos.append(B.index(min(i)))
print(minimos)
print(indminimos)

, which results in:

[0.2, 0.1, 0.4]
[0, 8, 6]
  • When I do the minimum instead of the sum I do so: minimos = [] lists_min = [] for i in range(Len(Indice): value = A[Indice[i]] lists_min.append([]) for j in range(Len(A): IF a[j]==value: #minimos.append(min(I_ac2[j])) lists_min[i]. append(B[j]) minimos.append(min(lists_min[i])) print minimos , and it works, gives me the minimum value for each set. I also tried to return the respective Dice from the original list (A) and not in Asemrepecides where this happens, through . index but I could not...

  • @Sofiaraimundo It is quite difficult to understand code from a comment. Edit your question or open a new question (I suggest you open up new questions for new questions otherwise the posts get confused and unnecessarily long).

  • I’ve already edited, so my question now is just the last part, related to the @armatita minimum

Browser other questions tagged

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