Correlate vectors through the index and assign different values

Asked

Viewed 44 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 DIFFERENT 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 DIFFERENT values:

posicao_2 = [43, 61, **22, 30**, 46, 63, 68, 72, 66, 89, **31, 41**, 8, 7, 6, 15]

I have the script below, which adds EQUAL values:

    ================================
    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]

    aleatorio = randrange(0, 100)

    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)

    print(posicao_2)        
    ==============================

How to modify to add DIFFERENT VALUES?

Would someone please help me?

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

  • 1

    posicao_2[i: i+len(p)] = random.sample(range(100), len(p) ) or posicao_2[i: i+len(p)] = [random.randrange(0,100)] * len(p) if it is to be different two by two

  • The algorithm worked correctly, but what I need is the following In the indexes [2, 3] and [10, 11] of the vector posicao_2 it assigns tuples with different values, for example: [31,10]. In indexes [2, 3] and [10,11]. For example, it should look like this: posicao_2 = [43, 61, 31, 10, 46, 63, 68, 72, 66, 89, 31, 10, 8, 7, 6, 15]

  • That is, the tuple must have different values, but the tuple must be equal.

  • I’m sorry if I’m not being too clear.

  • You got the idea @Isac?

  • For example: Using this logic you created: for i in indices: posicao_2[i: i+Len(p)] = [aleatorio] * Len(p) The values [2,3] and [10,11] are assigned [16,16] and [16, 16] I wish that in the positions [2,3] and [10,11] the same values are assigned, but the tuple must be different. For example, instead of [16, 16] and [16,16] be [31,10] and [31, 10].

  • 1

    I advise you to deepen the study on programming so as not to get stuck in exchanges that are considerably simple. This will also help you to develop new logics for your goals easily and not depend on anyone. It will be valid for both python and any other language since the important thing is logic.

  • Thank you @Isac. You’re right. I’m already doing this.

Show 3 more comments

1 answer

1


If you need each "Tuplo" found to be replaced by an equal random value in the two positions but different from successive tuples, just generate a new random before each exchange:

for i in indices:
    aleatorio = random.randrange(0,100)
    posicao_2[i: i+len(p)] = [aleatorio] * len(p)

Browser other questions tagged

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