在 python 中生成组合
我不知道如何在 Python 中解决这个问题,如果可能的话。我需要做的是从 3 个独立的数组创建一个数组(或矩阵,或向量?)。每个数组有 4 个元素,它们返回:
Class1 = [1,2,3,4] 类别 2 = [1,2,3,4] Class3 = [1,2,3,4]
现在我想做的是返回这三个类的所有可能组合。
示例:
1 1 1
2 1 1
3 1 1
4 1 1
1 2 1
2 2 1
3 2 1
4 2 1...
...依此类推到64行(4个元素*每个类16种可能的组合= 64行
我希望有一种方法可以在python中做到这一点。我确信有,但我没有确定最有效的方法是什么?也许是迭代每个类的每个元素的“for in”循环语句?或者现在我正在研究这个,itertools 可以处理这个问题吗
?
I am not sure how to go about this in Python, if its even possible. What I need to do is create an array (or a matrix, or vector?) from 3 separate arrays. Each array as 4 elements as such, they return this:
Class1 = [1,2,3,4]
Class2 = [1,2,3,4]
Class3 = [1,2,3,4]
Now what I would like to do is return all possible combinations of these three classes.
Example:
1 1 1
2 1 1
3 1 1
4 1 1
1 2 1
2 2 1
3 2 1
4 2 1...
...and so on to 64 rows (4 elements *16 possible combinations for each class = 64 rows
I am hoping there is a way to do this in python. I am sure there is but I am not sure what the most efficient way to go about would be. Perhaps a "for in" loop statement that iterates over each element for each class? Or now that I am researching this, would itertools handle this?
Thanks in advance for any help offered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要的称为 笛卡尔积:
What you want is called a Cartesian product:
最简单的方法:
The simplest way:
检查Python
itertools
标准模块:Check the Python
itertools
standard module: