I had the list:
A = [12, 15, 10, 15, 12, 10, 10, 10, 15, 12, 12, 15, 15, 15]
, when doing:
ASemRepetidos=set(A)
I get ([10, 12, 15])
, as seen, if in A
my first element is 12, in set(A)
becomes 10, or I lose the original order of 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]
When you make 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])
obtaining from the list B
the values of the 1st index that repeats.
But if instead of just wanting the value of the 1st repeated index I want the sum of each repeated set but of all sets, and keeping the order of occurrence of the key of those sets in A
and not set(A)
how do I?
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 sort of 10-12-15
, whereas I needed the order to hold: 12-15-10
, because then the search results of the elements by index in the list are not consistent with those presented in this sum function and the idea is that the values are in the end passed to an Excel by the way they appear initially.
If instead of the sum you want the minimum of each set I do:
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, give me the minimum value for each set. I tried to also return the index of the original list ( A
) and not ASemRepetidos
where it happens, through .index
but could not.