lundi 11 juin 2018

In Python: How to use the index number in an if-statement that is nested in a for-loop?

My question is about the fact that I have a simulated dataset, that contains two vectors. Hence, I have a vector vX and a vector vY. The problem I give is an example that resembles my issues, since my main code is too long. It is written as a function, as that is what I need in the end.

The problem at hand is that my vectors are ordered. So the element vX[0] should correspond to vY[0], and so on. The idea is that I need all the elements of vX that belong in a certain interval, acquire their index number and fill a new vector with the corresponding vY values.

Thus far I have written this:

vX = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
vY = [2, 14, 25, 12, 3, 52 , 5, 10, 7, 19]

vN = []

def(rndf(X, Y)):
    for i in X:
        if i in range(3, 6):
            vN.append(vY[i])
        else:
            vN = vN

vnY = rndf(vX, vY)

In this case the if-statement only holds true for vX = 3, 4 and 5. Then I want to have the corresponding values for vY in the vN vector, i.e. vN = [25, 12, 3]. Hopefully someone understand the problem and is able to help me. Thank you in advance.

Aucun commentaire:

Enregistrer un commentaire