Relation between Python Arrays

Asked

Viewed 89 times

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:

  • heading = [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]

For each 4 coordinate vector indices, it corresponds to 1 position vector index.

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

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

What I wish to do is this:

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

For example:

  • I have the following vectors:

Heading = [40.51, 30.52, 30.31]

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

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

position = [40.51, 30.52, 30.31]

  • Which correspond to these 4 coordinate vector indices.

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

  • I must replace the values of the coordinate vector with these:

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

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

Excuse me if I’m not quite clear, I tried to be as didactic as possible.

I ask you to help me, I really don’t know how I can fix it. It seems simple to me, but I’m still beginner.

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

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 you access the second array multiplying the position by 4 and accessing 4 elements at a time. For each of the accessed elements generates a new one using the randrange 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 working on Ideone to test

  • Isac, I’m really grateful for your help. Thank you so much for taking the time to help me. Thank you very, very much!

  • @Danilo No problem, good development rest.

Browser other questions tagged

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