【问题标题】:How would I generate every possible combination of 2 lists [duplicate]我将如何生成 2 个列表的所有可能组合 [重复]
【发布时间】:2021-06-03 17:33:31
【问题描述】:

所以我会有 2 个列表,其中包含随机数量的值。 例如:

listx = [15513, 813, 984949, 5000], listj = [76815, 75, 8484, 9419419, 418841814, 84848, 84848]

如何生成这些列表的所有可能组合? 喜欢:

combination = [15513, 75, 9419419]

它可以从两个列表中获取尽可能多的值以用于组合,我想检查每个组合,就像它可以从第一个中获取 2 个值,从另一个中获取 5 个值,或者从第一个中获取所有值一个和三个从另一个。

【问题讨论】:

  • 您似乎忘记包含您的尝试?
  • 查看itertools(组合、排列、乘积等)
  • 您可以查看如何在 Python 中进行组合并编写代码。见How much research?。如果您对该代码有疑问,那么您可能会遇到 Stack Overflow 问题。
  • 我没有尝试,因为我不知道从哪里开始

标签: python python-3.x iteration combinations


【解决方案1】:

尝试:

from itertools import combinations, chain

listx = [15513, 813, 984949, 5000]
listj = [76815, 75, 8484, 9419419, 418841814, 84848, 84848]

for i in range(2, len(listx) + len(listj)):
    print(list(combinations(chain(*[listx,listj]), i)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多