Elements of B whose indices are the positions of occurrences of equal elements in A

1

If I have the lists:

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]

How can I group the values of B that are in the repeated element positions in A into separate lists? Shown for example:

nova_lista(12) = [0.2, 0.2, 0.3, 0.7]
nova_lista(15) = [0.3, 0.2, 0.1, 0.4, 0.5, 0.5]

I've tried this code but it does not make sense either.

nova_lista_12 = [x for x in A if A[x]==A[x+1]]
print nova_lista_12
    
asked by anonymous 06.04.2016 / 16:58

2 answers

1

First, let's create a collection from A without repeated values:

ASemRepetidos = set(A)

For each element of this set, we will find the indexes of its occurrences in A. When you need indexes, the enumerate function is your friend. With her help and a list comprehension , we set up this function:

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

So we need to get the values of B corresponding to these positions. Let's do another function for this:

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

With these functions, let's go to our program:

The most appropriate data structure to generate variables "dynamically", as you are wanting with your nova_lista(x) is mapping , or, in Python parlance, a dictionary in>. Let's create a dictionary whose keys are the unique elements of A and values are the corresponding values of B. With the above functions, it's simple:

dicionarioResultante = {} # coloque um nome mais adequado ao seu contexto

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

print(dicionarioResultante)

Applying this to your input sample, we have the output:

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

An example of using the result:

for chave in ASemRepetidos:
    print("Número: " + str(chave) + ". Valores correspondentes de B: " + str(dicionarioResultante[chave]))

Or, using only dictionary methods (more interesting if your code is modular and B is out of scope):

for chave in dicionarioResultante.keys():
    print("Número: " + str(chave) + ". Valores correspondentes de B: " + str(dicionarioResultante[chave]))
    
06.04.2016 / 18:33
0
>>> 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]
>>> 
>>> D = {}
>>>
>>> for (i,key) in enumerate(A):
...  D.update({key:[B[i]] if not D.has_key(key) else D[key]+[B[i]]})
... 
>>> print D
{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]}
>>> print D[12]
[0.2, 0.2, 0.3, 0.7]
>>> print D[15]
[0.3, 0.2, 0.1, 0.4, 0.5, 0.5]
    
20.04.2016 / 22:14