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

Asked

Viewed 272 times

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 in separate lists the values of B that are in the positions of elements repeated in A? Displayed 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 tried this code but besides giving error also does not make sense to me...

nova_lista_12 = [x for x in A if A[x]==A[x+1]]
print nova_lista_12
  • It has no value of B that is equal to that of A. I believe there are functions to make an intersection, difference, union of lists. I could explain better and if possible make an example in ideone?

  • What I intend is, to find the indices of A whose elements are equal (whenever they are 12 or whenever they are 15, or 10) and in a new list put the values of B of these repeated indices. Thus for the value 12 of A, it would have a list: nova_lista_12=[0.2, 0.2, 0.3, 0.7] with the B values of the indices where A is 12...

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 index of its occurrences in A. When you need indexes, the function enumerate is your friend. With her help and a comprehensilist on, we set up this function:

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

With this, 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, we go to our program:

The most suitable data structure to generate variables "dynamically" as you want with your nova_lista(x) is the mapping, or, in Python’s language, a dictionary. 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 example, 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 modularized and B is out of scope):

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

    Thank you, in fact, do what I need!! But I don’t really know how dictionaries work.. How can I for example add the values that belong to 10 ? If I add Sultante.values() I will get the lists of 10, 12 and 15, right? how I’m only going to get the 10 and then add up the elements?

  • dictionaryResultant[10]. To add, use the sum function.

  • And there is no more general form of the dictionaryResultant[Indice] type, because if the list is too large with many key/value pairs it becomes something easier to do?

  • I don’t understand. What’s wrong with that shape?

  • It was to make the code applicable to other studies, if the values of the lists change all and the lists are larger. Instead of having to view list A to know which keys, if there was a way to write instead of dictionaryResultant[10], for example with a cycle that ran through Asemrepetidos, since this list is the key list

  • Yeah, sure. The idea is you make one for in ASemRepetidos and make dicionarioResultante[elementoDeASemRepetidos] . I left that part out because there is nothing similar in your code. It is similar to a list. I will put an example.

  • @Sofiaraimundo Ready. Take a look.

  • That’s exactly what I was trying to ask, thank you!!

  • How can I know the index of the repeated the first time they appear in the original list A and not their index in the list without repeats (Asemrepetidos)? That is, I wanted a function that returned me that the index of 12 is 0, that the of 15 is 1 and that the of 10 is 2?

  • suaLista.index(valorBuscado). Avoid asking new questions in the comments. Use this field only to ask for clarification or supplement an answer. If you have a new question, open a new question (and look before if it is not already answered).

  • but I wanted to return all key indexes, not to do one by one and be more general, I tried this code but did not give... Indice=[] for k in range(0,Len(A): for key in Asemrepetida: Indice.append(A.index(key)) print Indice

  • One of the functions I wrote in the answer does not exactly that?

Show 8 more comments

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]

Browser other questions tagged

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