Relation between Python Arrays

1

I'm a beginner in Python and I'm having a hard time doing the following:

I have 1 vector with the following values:

  • position = [34.53, 32.64, 44.20, 43.41]

These values are given by the Euclidean distance of the following coordinates:

  • coordinates = [29, 83, 61, 70, 29, 83, 50, 58, 29, 83, 64, 56, 29, 83, 71, 72]

Every 4 indices of the coordinate vector corresponds to 1 index of the position vector.

  • For example: The value [34.53] of the position vector corresponds to the values [29, 83, 61, 70] of the coordinate vector.

==================

What I want to do is the following:

When a value of my position is greater than 40.00, I must modify the corresponding coordinates in the coordinates vector by other values defined at random.

For example:

  • I have the following vectors:

position = [40.51, 30.52, 30.31]

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

  • I found a value greater than 40.00 in the position vector.

position = [ 40.51 , 30.52, 30.31)

  • That correspond to these 4 indexes of the coordinate vector.

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

  • I need to replace the coordinate vector values with these:

Coordinates = 200, 130, 67, 31 , 50, 60, 70, 80, 90, 100, 110, 120]

=================

I'm sorry if it's not very clear yet, I've tried to be as educational as possible.

I ask you to help me, I really do not know how I can resolve this. Sounds simple to me, but I'm still a beginner.

I am very grateful to those who are willing to help me.

    
asked by anonymous 05.08.2017 / 03:50

1 answer

1

To achieve this you can traverse the first array through the position and test whether the value is above 40 with a if . If it is accesses the second array by multiplying the position by 4 and accessing 4 elements at a time. For each of the elements accessed, generate a new one using randrange of python:

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: #se maior que 40
         for indice2 in range(indice*4, indice*4+4): #aceder aos correspondentes        
            coordenadas[indice2] = random.randrange(0,200) #gerar os aleatorios entre 0 e 200


print(coordenadas)

Example running Ideone to test

    
05.08.2017 / 11:35