使用 itertools 的特殊类型组合

发布于 2024-09-14 22:26:27 字数 589 浏览 6 评论 0原文

我几乎完成了某人给我的一项任务,该任务最初涉及轻松使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

[旋木] 2024-09-21 22:26:27

我想知道你执行的神奇计算是什么,但看起来这就是你的公式:

k = int(raw_input('From What row items should be appeared again at the end?'))
res = [l for l in product(*(li+[li[k]])) if l[k]<l[len(li)] ]

I wondering what is the magical computations you performing, but it look's like that's your formula:

k = int(raw_input('From What row items should be appeared again at the end?'))
res = [l for l in product(*(li+[li[k]])) if l[k]<l[len(li)] ]
西瓜 2024-09-21 22:26:27

泛化为两个以上的子列表(映射函数将是另一种选择)

from pprint import pprint
for li in ([[1, 2, 3],
            [4, 5, 6]],

           [[1,  2,  3,  4],
            [5,  6,  7,  8],
            [9, 10, 11, 12]]
           ):
    triples= []
    prevlist=li[0]
    for nextlist in li[1:]:
        for spacing in range(1,len(prevlist)):
            triples.extend([[first,other,second]
                            for first,second in zip(prevlist,prevlist[spacing:])
                            for other in nextlist])

    pprint(sorted(triples))

Generalized for more than two sublist (map function would be the other alternative)

from pprint import pprint
for li in ([[1, 2, 3],
            [4, 5, 6]],

           [[1,  2,  3,  4],
            [5,  6,  7,  8],
            [9, 10, 11, 12]]
           ):
    triples= []
    prevlist=li[0]
    for nextlist in li[1:]:
        for spacing in range(1,len(prevlist)):
            triples.extend([[first,other,second]
                            for first,second in zip(prevlist,prevlist[spacing:])
                            for other in nextlist])

    pprint(sorted(triples))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文