【问题标题】:Scenarios from K number of designs in N number of positions - Python来自 N 个位置的 K 个设计的场景 - Python
【发布时间】:2021-06-13 12:17:45
【问题描述】:

我正在尝试生成所有可能的场景,我可以将k 的设计数量放在n 的位置数量中。这应该给出 k^n 个场景。比如k = 2n = 3,所有的场景都是:

n1 n2 n3
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2

所以必须有 8 (2^8) 个场景。

我已经尝试过来自itertoolscombinationspermutationscombinations_with_replacement,但似乎都没有给出我想要的解决方案。

是否有 python 函数来生成这个?

【问题讨论】:

    标签: python-3.x combinations combinatorics


    【解决方案1】:

    您正在寻找产品:

    for p in itertools.product(range(1,3),repeat = 3):
        print(p)
    
    (1, 1, 1)
    (1, 1, 2)
    (1, 2, 1)
    (1, 2, 2)
    (2, 1, 1)
    (2, 1, 2)
    (2, 2, 1)
    (2, 2, 2)
    

    【讨论】:

    • 哦,是的,不仅仅是product。太棒了,非常感谢。非常感谢这位先生:)
    猜你喜欢
    • 2021-10-22
    • 1970-01-01
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多