【问题标题】:Passing list of arguments of varying length [duplicate]传递不同长度的参数列表[重复]
【发布时间】:2015-03-14 22:01:29
【问题描述】:

这可能是一个愚蠢的问题,所以我会尽可能简单。

我有一个包含不同数量列表 (x) 的列表,我正在尝试将每个列表传递给函数 itertools.product(),但我无法让它工作。

x = [[a, b, c], [d, e, f], [g, h, i]]
itertools.product(x[0], x[1], x[2]...)

我试过了:

itertools.product(x[n] for n in range(len(x)))
itertools.product(n for n in x)

我的函数有望表现良好:

output = []
for product in itertools.product(x[n] for n in range(len(x))):
    output.append(product)
output """--> [(a, d), (a, e)...]"""

非常感谢。

【问题讨论】:

    标签: python


    【解决方案1】:

    使用unpacking:

    x = [[a, b, c], [d, e, f], [g, h, i]]
    itertools.product(*x)
    

    【讨论】:

      【解决方案2】:

      Same as always.

      itertools.product(*x)
      

      【讨论】:

        猜你喜欢
        • 2018-05-08
        • 1970-01-01
        • 2017-06-22
        • 2017-10-13
        • 2016-09-05
        • 2022-01-11
        • 1970-01-01
        • 1970-01-01
        • 2022-10-07
        相关资源
        最近更新 更多