【发布时间】:2021-06-13 12:17:45
【问题描述】:
我正在尝试生成所有可能的场景,我可以将k 的设计数量放在n 的位置数量中。这应该给出 k^n 个场景。比如k = 2和n = 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) 个场景。
我已经尝试过来自itertools 的combinations、permutations 和combinations_with_replacement,但似乎都没有给出我想要的解决方案。
是否有 python 函数来生成这个?
【问题讨论】:
标签: python-3.x combinations combinatorics