繁体   English   中英

如何迭代笛卡尔积,使顶级项首先组合?

[英]How to iterate the cartesian product so top items combine first?

我需要获得iterables的笛卡尔积,就像itertools.product给我的那样,但是出于优化的原因,我希望索引总和最低的那些对/组合首先出现。

因此,例如,如果我有两个列表, a = [1, 2, 3, 4, 5]b = ['a', 'b', 'c', 'd', 'e'] ,则itertools.product给我:

>>> list(itertools.product(a, b))
[(1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (1, 'e'), (2, 'a'), (2, 'b'), (2, 'c'), (2, 'd'), (2, 'e'), (3, 'a'), (3, 'b'), (3, 'c'), (3, 'd'), (3, 'e'), (4, 'a'), (4, 'b'), (4, 'c'), (4, 'd'), (4, 'e'), (5, 'a'), (5, 'b'), (5, 'c'), (5, 'd'), (5, 'e')]

相反,我想在(1, 'c')之前看到(2, 'a') (1, 'c') 在例如(1, 'b')(2, 'a')之间的确切顺序并不重要。


目前,我正在根据索引范围的乘积对列表进行排序:

>>> sorted(list(itertools.product(range(len(a)), range(len(b)))), lambda a, b: sum(a) - sum(b))
[(0, 0), (0, 1), (1, 0), (0, 2), (1, 1), (2, 0), (0, 3), (1, 2), (2, 1), (3, 0), (0, 4), (1, 3), (2, 2), (3, 1), (4, 0), (1, 4), (2, 3), (3, 2), (4, 1), (2, 4), (3, 3), (4, 2), (3, 4), (4, 3), (4, 4)]

然后使用它来索引列表。 但是,长列表占用了太多内存。 我需要某种生成器,其调用约定与itertools.product相同,但我无法弄清楚迭代的方式,因此我只能一次获得排序和所有可能的对。

在@otus注释之后更新-生成按总和排序的索引,使用这些索引查找值:

A = range(5)
B = 'abcde'

def indices(A,B):
    # iterate all possible target sums in order
    for m in range(max(A)+max(B)):
        for a in A:
            # stop once current target sum isn't possible
            if a > m:
                break
            # yield if sum equals current target sum
            if m-a in B:
                yield a,m-a

def values(A,B):
    for a,b in indices(range(len(A)),set(range(len(B)))):
        yield A[a],B[b]

print list(values(A,B))

输出:

[(0, 'a'), (0, 'b'), (1, 'a'), (0, 'c'), (1, 'b'), (2, 'a'), (0, 'd'), (1, 'c'), (2, 'b'), (3, 'a'), (0, 'e'), (1, 'd'), (2, 'c'), (3, 'b'), (4, 'a'), (1, 'e'), (2, 'd'), (3, 'c'), (4, 'b'), (2, 'e'), (3, 'd'), (4, 'c'), (3, 'e'), (4, 'd')]
def cartprod(x,y):
    nx = len(x)
    ny = len(y)
    for i in range(nx+ny):
        for j in range(max(0,i-ny+1), min(i+1,nx)):
            yield (x[j],y[i-j])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM