在Python 3中动态传递函数中的参数



当我调用函数时,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的迭代,产品是:

i=1:产品(l,l)

i=2:产品(l,l,l)

i=3:产品(l,l,l)

如果我能正确回忆的话,我所知道的唯一支持这种功能的语言就是PHP。

itertools.product接受可选关键字参数repeat:

所以,你可以做:

for n in itertools.product(l, repeat=i+1):
    ...

或者,要动态传递参数,可以使用*args(请参阅打开参数列表):

for n in itertools.product(*([l] * (i+1))):
    ...

最新更新