查找每个数字选自不同集合的所有组合
我需要生成数字序列的每种组合,其中每个数字都可以从有限集中选取。
例如:
(1|2)、(1|2)、(2|3)、(2|3)、(2|3)、(3|4|5)
其中第一位数字可以是 1 或2,最后一位数字可以是 3、4 或 5。1,2,2,3,2,4
是有效组合,但 3,2,2,3,2,4 则不是。
做到这一点最简单的方法是什么?
I need to generate every combination of a sequence of digits where each digit can be picked from a finite set.
For example:
(1|2), (1|2), (2|3), (2|3), (2|3), (3|4|5)
Where the first digit can be a 1 or a 2, and the last digit can be a 3, 4, or 5.
1,2,2,3,2,4 would be a valid combination, but 3,2,2,3,2,4 wouldn't.
What's the easiest way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
itertools.product 正是这样做的:
itertools.product does exactly that: