【问题标题】:Python - Dynamically change number of arguments [duplicate]Python - 动态更改参数数量[重复]
【发布时间】:2023-04-08 21:14:01
【问题描述】:

我想根据不断变化的集合数计算笛卡尔积。

可以使用itertools.product(*args, repeat=1) 计算笛卡尔积

itertools.product(set1, set2, repeat=1)

但是集合的数量实际上是未知的,这取决于数据。有时可能是 2 组,有时是 3 组或更多。

有没有办法提交 *args 以便它可以动态更改参数的数量?

myargs = [set1, set2, set3]
itertools.product(myargs,repeat=1)

【问题讨论】:

    标签: python python-3.x itertools


    【解决方案1】:

    是的,有这样的方法。使用* 参数扩展运算符:

    myargs = [set1, set2, set3]
    itertools.product(*myargs,repeat=1)
    

    参考:

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 2010-10-15
      • 2018-08-24
      • 2020-06-07
      • 1970-01-01
      • 2019-06-03
      • 2019-12-17
      • 2020-08-30
      • 2013-05-11
      相关资源
      最近更新 更多