Initial input of a list after using set

Asked

Viewed 131 times

0

I used this code initially to aggregate in dictionaries the values of B which had equal value in A:

A = [12,   15,  10,  15,  12,  10,  10,  10,  15,  12,  12,  15,  15,  15]
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]

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)

And the result was:

{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]}

But in doing so, set(A) changes the order of the elements and I needed the original order of A to find in a 3rd list the values of this one that had the same information that A and not that ASemRepetidos, because this list is much smaller than A, in addition to being "disorderly" due to the set application. How can I know the content of repeated the first time they appear in the original list A and not your index on the no-repeat list (ASemRepetidos)? That is, I wanted a general function that would return me that the index of 12 is 0, that the of 15 is 1 and that the of 10 is 2?
I tried to make:

indice=[] 
for k in range(0,len(A)):
    for chave in ASemRepetidos: 
        indice.append(A.index(chave)) 
print indice

but it did not give...
Anyone can help?

  • Why A.index(valor) didn’t work out?

  • that’s not what I want, I expressed myself badly.. when applying set this disorderly me the initial lists... Imagine that the list A is a code, and therefore whenever this code is equal, the corresponding elements of the list B and C, etc are equal.. and in the end I only care that the program presents 1 value (of the several that are equal), for each key and for each list, you understand my doubt and what I intend to do?

  • But set() does not modify the original list.

1 answer

0


If I understand correctly what you’re trying to do next should solve the problem:

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)
print(indice)
print(indic)

Note that I have put a dictionary that is not necessary to solve the problem but helps to confirm the values requested by you. The dictionary has as key the values of A and as value the indices of the occurrences. The result is:

[0, 1, 2]
{10: 2, 12: 0, 15: 1}
  • Now I needed to keep this order of indices, but add the values whenever the code is the same, that is whenever the value in A is the same.. I already had the dictionary done and for this case 10: [1.1, 0.7, 0.4, 0.6], it would be enough to make sum(1.1, 0.7, 0.4, 0.6), but as the set was applied, this sorts the values in a strange way and does not give me the occurrence of each one in the order it appears in the original list.. That is, by his reply I removed the necessary indices to go looking for their values in a list C, but the indices of the dictionary do not correspond to these.. what should I do?

  • @I’m not getting the problem. I think it’s best to open a new question with the code that’s giving you the problem as well as the output you expect to have. Put the link here I try to see how it resolves.

  • here you are: http://answall.com/questions/122264/desordem-lista-ao-aplica-set @armatita

Browser other questions tagged

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