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.
– armatita
Where exactly do I keep the code? It’s not giving...
– Sofia Raimundo
Next to lists[i]. append(B[j]). Make another list like "lists" to store the indices.
– armatita
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...
– Sofia Raimundo
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.
– armatita