Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python的for循环中使用多个iter变量_Python_Loops - Fatal编程技术网

在python的for循环中使用多个iter变量

在python的for循环中使用多个iter变量,python,loops,Python,Loops,我是python新手,我有以下代码: def multiply(x,y): return x*y sequence = [args.x, args.y] # args.x and args.y come from the command line combinations = it.product(*sequence) for argx,argy in combinations: print (multiply(argx,argy)) 我的问题是,我有几十个参数,比如arg

我是python新手,我有以下代码:

def multiply(x,y):
    return x*y

sequence = [args.x, args.y] # args.x and args.y come from the command line
combinations = it.product(*sequence)

for argx,argy in combinations:
    print (multiply(argx,argy))
我的问题是,我有几十个参数,比如argx和argy,我应该在for循环中提到它们,这样我就可以用它们的名字来引用它们,但看起来不太好。 我不太明白我应该做什么来避免把它们都列在一个原始列表中。 我还想通过名称而不是索引来引用所有变量。 我觉得一定有一些简单的解决办法,但我找不到。
另外,我的实际代码与乘法函数没有任何关系,上面的代码只是我实际代码的一个模型。

您可以使用
zip
iterable并行迭代两个列表:

xx = [1,2,3,4]
yy = [10,20,30,40]
for x,y in zip(xx,yy):
   print x + y
导致:

11
22
33
44

请参见Python文档中的:

它是什么.product()?噢,它代表itertools.product(),itertools模块作为它的一部分导入。