python-3.x - 来自 N 个位置的 K 个设计的场景 - Python
问题描述
我正在尝试生成所有可能的场景,我可以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) 个场景。
我已经尝试过,combinations
但似乎都没有给出我正在尝试的解决方案。permutations
combinations_with_replacement
itertools
有python
生成这个的函数吗?
解决方案
您正在寻找产品:
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)
推荐阅读
- itext - Itext 5 - 分离签名 - getRangeStream() 返回的变量 byte[]
- scala - Spark(Scala)中的灵活连接条件
- frama-c - 为什么使用 WP 无法验证这个简单的浮点算术属性?
- prolog - 为什么 GNU prolog 中的“重复”不能按预期工作?
- c++ - 无法在 C++ 上为 OpenWrt 编译包 Helloworld
- android - Instabug 实例永远不会被破坏 [Android]
- javascript - Semantic UI React:如何从搜索框中隐藏建议的文本
- javascript - Nativescript(角度)找不到延迟加载的模块
- linux - Azure Pipeline Agent 无法使用代理并出现 Curl 错误:CURLE_RECV_ERROR
- javascript - 使用redux登录后如何在反应路由器dom中重定向到主页?