简体   繁体   English

遍历列和数组

[英]Iterating through columns and array

In python I'm trying to loop through two columns and an array at at the same time. 在python中,我试图同时遍历两列和一个数组。 I was playing with the zip function but without success. 我在玩zip功能,但没有成功。

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. 我的函数i,v和temp有三个输入参数,每次我想运行该函数时,都需要手动更改i,v和temp。 Now I want to build a loop that will return me the columns of I and V and the values of temp. 现在,我想构建一个循环,该循环将返回I和V的列以及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. 不确定如何组合列和数组。

You can do this in many ways, eg: 您可以通过多种方式执行此操作,例如:

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) . 如果您需要示例中的元组,则可以使用tuple(parameter)重铸tuple(parameter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM