在Python 3中动态地在函数中传递参数
python是否支持在调用函数时传递动态参数在Python 3中动态地在函数中传递参数,python,python-3.x,arguments,parameter-passing,Python,Python 3.x,Arguments,Parameter Passing,python是否支持在调用函数时传递动态参数 import itertools.product l = [1,2,3,95,5] for i in range(5): for n in itertools.product(l,l): #calculations that #reduce set size 我希望通过迭代,我的产品是: i=1:乘积(l,l) i=2:乘积(l,l,l) i=3:乘积(l,l,l,l) 如果我没记错的话
import itertools.product
l = [1,2,3,95,5]
for i in range(5):
for n in itertools.product(l,l):
#calculations that
#reduce set size
我希望通过迭代,我的产品是:
i=1:乘积(l,l)
i=2:乘积(l,l,l)
i=3:乘积(l,l,l,l)
如果我没记错的话,我所知道的唯一支持这种功能的语言就是PHP。接受一个可选的关键字参数repeat
:
因此,您可以:
for n in itertools.product(l, repeat=i+1):
...
或者,要动态传递参数,可以使用
*args
(请参阅):
for n in itertools.product(*([l] * (i+1))):
...