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?– Pablo Almeida
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?
– Sofia Raimundo
But
set()
does not modify the original list.– Pablo Almeida