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!
posicao_2[i: i+len(p)] = random.sample(range(100), len(p) )
orposicao_2[i: i+len(p)] = [random.randrange(0,100)] * len(p)
if it is to be different two by two– Isac
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]
– Danilo
That is, the tuple must have different values, but the tuple must be equal.
– Danilo
I’m sorry if I’m not being too clear.
– Danilo
You got the idea @Isac?
– Danilo
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].
– Danilo
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.
– Isac
Thank you @Isac. You’re right. I’m already doing this.
– Danilo