使用 itertools 的特殊类型组合
我几乎完成了某人给我的一项任务,该任务最初涉及轻松使用 itertools 中的 Product() 函数。 然而,该人要求它也应该做一些不同的事情,例如:
li =
[[1, 2, 3],
[4, 5, 6]]
常规的 Product() 会给出如下结果: [1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2 , 6], [3, 4] ...
它应该做的是:
做一个常规的product(),然后,从列表中的第一个元素添加下一个项目,依此类推。完整的示例集是:
[[1, 4, 2]
[1,4,3],
[1, 5, 2],
[1,5,3],
[2,4,3],
[2,5,3],
[2,6,3]]
这种情况下应该如何使用itertools?
编辑:
如果我解释该计划的目标可能会有所帮助: 例如,用户将输入 5 行 x 6 列的数字列表。
普通的product()会产生5个数字的组合。这个人想要一个 6 位数的组合。这个“第6”数字从何而来?这将取决于他对他想要的哪一行的选择。
I am almost finished with a task someone gave me that at first involved easy use of the product() function from itertools.
However, the person asked that it should also do something a bit different like:
li =
[[1, 2, 3],
[4, 5, 6]]
A regular product() would give something like: [1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4] ...
What it should do is:
Do a regular product(), then, add the next item from the first element in the list and so on. A complete set of example would be:
[[1, 4, 2]
[1, 4, 3],
[1, 5, 2],
[1, 5, 3],
[2, 4, 3],
[2, 5, 3],
[2, 6, 3]]
How should I use itertools in this circumstance?
EDIT:
It might help if I explain the goal of the program:
The user will enter, for example, a 5 row by 6 column list of numbers.
A normal product() will result in a 5-number combination. The person wants a 6-number combination. Where will this "6th" number come from? It would come from his choice of which row he wants.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想知道你执行的神奇计算是什么,但看起来这就是你的公式:
I wondering what is the magical computations you performing, but it look's like that's your formula:
泛化为两个以上的子列表(映射函数将是另一种选择)
Generalized for more than two sublist (map function would be the other alternative)