【问题标题】:Generate all possible combinations in Python在 Python 中生成所有可能的组合
【发布时间】:2014-02-16 00:06:11
【问题描述】:
     Choice1   Choice2   Choice3
A       1         5         9
B       2         6         10
C       3         7         11
D       4         8         12

我如何生成所有可能的组合,包括 python 中每一行的一个选项?

【问题讨论】:

  • 不知道您是否需要特定于 pandas 的解决方案,但如果没有 pandas,那就是 itertools.product(*rowlist)。
  • 查看您的列名,我觉得可能有更好的方法来做您想做的事。您在这里的最终目标是什么?

标签: python pandas combinations


【解决方案1】:

你可以试试这个得到笛卡尔积。

import itertools

rowlists = [[1,5,9],[2,6,10],[3,7,11],[4,8,12]]
for combination in itertools.product(*rowlists):
    print combination

【讨论】:

    猜你喜欢
    • 2016-08-16
    • 2017-09-15
    • 1970-01-01
    • 2016-01-07
    • 2021-01-14
    相关资源
    最近更新 更多