Correlating vectors through the Python indexes

Asked

Viewed 340 times

0

I have the following vectors:

posicao_1 = [4, 62, 24, 18, 47, 62, 63, 78, 68, 87, 24, 18, 6, 8, 12, 17]

posicao_2 = [43, 61, 21, 19, 46, 63, 68, 72, 66, 89, 29, 10, 8, 7, 6, 15]

I need to search the vector POSICAO_1 the value [24, 18].

posicao_1 = [4, 62, **24, 18**, 47, 62, 63, 78, 68, 87, **24, 18**, 6, 8, 12, 17]

We can observe that they are found in the indexes [2, 3] and [10, 11] of the vector POSITIO_1.

After locating these values, I need to go through the POSIO_2 vector and assign random and EQUAL values in the INDEXES [2, 3] and [10, 11] that I located in the POSIO_1 vector. For example:

Would have to assign random and EQUAL values:

posicao_2 = [43, 61, **21, 21**, 46, 63, 68, 72, 66, 89, **21, 21**, 8, 7, 6, 15]

Would someone please help me?

I’m sorry, in case I wasn’t clear, I tried to explain as much as possible!

1 answer

1


Starting from the code of your last question you just need to create a list of indices and save the elements where that code is placing the random ones. Then use it to replace in the second vector posicao_2

Thus:

import random
aleatorio = random.randrange(0,200)

posicao_1 = [4, 62, 24, 18, 47, 62, 63, 78, 68, 87, 24, 18, 6, 8, 12, 17]
posicao_2 = [43, 61, 21, 19, 46, 63, 68, 72, 66, 89, 29, 10, 8, 7, 6, 15]
indices = [] #lista criada aqui

for j in range(len(posicao_1)):
    if posicao_1[j] in [24,18]:
        posicao_1[j] = aleatorio
        indices.append(j) #indice adicionado aqui, quando coloca aleatorios em posicao_1

for i in indices: #atribuição em posicao_2 com base nos indices
    posicao_2[i] = aleatorio

Edit

So that the exchange is done only by researching [24,18] as a sub-array is already necessary to modify a little the logic that was being made.

...
#imports e posições para cima iguais 
pesq = [24,18]

#construir os indices verificando se a partir de cada posição o subarray existe no principal
indices = [x for x in range(len(posicao_1)) if posicao_1[x:x+len(pesq )] == pesq ]

#para cada indice substituir o subarray pela mesma quantidade de aleatorios
for i in indices:
    posicao_2[i: i+len(pesq)] = [aleatorio] * len(pesq)

To search multiple tuples just transform the search array into an array of tuples and create another one for:

pesq = [[24,18], [62,63]]

for p in pesq:
    indices = [x for x in range(len(posicao_1)) if posicao_1[x:x+len(p)] == p ]

    for i in indices:
        posicao_2[i: i+len(p)] = [aleatorio] * len(p)
  • @Danilo I don’t know if I get the idea. Just change when they are followed is that? If they are removed it is no longer supposed to change?

  • Thank you for the answer. However, the following happens: If in my vector posicao_2 has some other value [24] or [18], it is also replaced. What I need is to replace only the positions where they are [24, 18] and ignore if there are other values [24] and [18] in other positions. sorry to bother you. But, if you can do that, it solves all my problems.

  • Exactly, @Isac

  • I need to change the position of the TUPLA (24, 18) and ignore the other positions where they appear [24] and [18].

  • managed to understand the idea?

  • 1

    @Danilo Check if you already get the results you expect

  • It is exactly this logic that I need, but if my "pesq" is for example [24, 18, 62, 63] it cannot modify.

  • That is, if I need to modify the indexes in the vector posicao_2 where the values are [24, 18] and [62, 63] it cannot. When my pesq has only 2 values, the algorithm works perfectly. However, when pesq has other tuples, it does not understand.

  • Sorry to bother you.

  • @Danilo The research is supposed to have only tuples so, something like pesq = [[2,3], [34,21], etc..] ? and find the index for when it matches the two values of each Tuple ?

  • For example, if pesq = [24, 18, 62, 63] the algorithm needs to exchange the indexes [2.3] and [ 5.6] of my vector posicao_2. .

  • Exactly @Isac.

  • Perfect!!!!!!!! Exactly what I need. You are to be congratulated. Thank you very, very much for your patience and attention. Not only in this, but in other questions of mine. Most grateful @Isac

  • how do I assign a tuple of different values, for example, instead of assigning [22, 22], assign [22, 30]?

  • @Danilo I didn’t understand the question, it has to be a little more specific

Show 10 more comments

Browser other questions tagged

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