Correlating values in Python Arrays

Asked

Viewed 70 times

1

I have a vector called POSITION, with the following values:

  • position = [40.51, 30.52, 30.31]

I have a vector called COORDINATES, with the following values:

  • coordinates = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]

Each index of the vector POSITION corresponds to another 4 of the coordinate vector. For example:

  • position = [40.51, 30.52, 30.31]

corresponds to:

  • coordinates = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]

I have the following script that, when detecting a value above 40.00 in the POSITION vector, assigns random values in the 4 corresponding positions in the COORDINATES vector.

import random

posicao = [40.51, 30.52, 30.31]
coordenadas = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]

for indice in range(0, len(posicao)):
    if posicao[indice]>40:
         for indice2 in range(indice*4, indice*4+4):
            coordenadas[indice2] = random.randrange(0,200) #gerar os aleatorios entre 0 e 200

print(coordenadas)

But I wish that instead of modifying the 4 values, only the last 2, for example:

  • position = [40.51, 30.52, 30.31]

Only modify:

  • coordinates = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120]

How do I modify this code to achieve this?

I am grateful to those who can help me. I believe it is something easy, but I cannot see the solution.

1 answer

2


For this it is only necessary to modify the second for to start 2 elements below.

Going from:

for indice2 in range(indice*4, indice*4+4):

To:

for indice2 in range(indice*4+2, indice*4+4): #Notar no +2 que foi adicionado
  • Thank you again! It worked perfectly.

Browser other questions tagged

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