0

In python I'm trying to loop through two columns and an array at at the same time. I was playing with the zip function but without success.

a=np.array(range(1,21))
b=np.array(range(4,24))
#DATA#####
i=a.reshape(4,5)
v=b.reshape(4,5)
temp=np.array(range(1,5))

I have three input parameters for my function i,v and temp, every time I want to run the function I need to change the i,v and temp manually. Now I want to build a loop that will return me the columns of I and V and the values of temp. In the first itteration my input needs to be the following:

i=1,2,3,4,5
v=4,5,6,7,8
temp=1

in the second itteration

i=6,7,8,9,10
v=9,10,11,12,13
temp=2

and so on

Not sure how to combine columns and array.

1 Answer 1

3

You can do this in many ways, e.g.:

for j in range(len(temp)):
    par0 = i[j]
    par1 = v [j]
    par2 = temp[j]
    # to check the output
    print('Iteration:', j, '\ni=', par0, '\nv=', par1, '\ntemp=', par2)

or

for j,par2 in enumerate(temp):
    par0 = i[j]
    par1 = v [j]
    # to check the output
    print('Iteration:', j, '\ni=', par0, '\nv=', par1, '\ntemp=', par2)

If you need a tuple like in your example you can recast the parameters with tuple(parameter).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.