Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何迭代笛卡尔积,使顶级项首先合并?_Python_Iteration_Itertools - Fatal编程技术网

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

Python 如何迭代笛卡尔积,使顶级项首先合并?,python,iteration,itertools,Python,Iteration,Itertools,我需要得到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')

我需要得到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,‘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 comment-生成按和排序的索引,使用这些索引查找值:

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')]

你的解决方案和@JulienBernu的解决方案都解决了我的问题,所以我很快就会接受其中一个。但是,它们都很容易扩展到常量输入,而不是*arg,并且需要序列作为输入,而不是生成器。@OTU您的要求是访问任意索引,因此任何生成器都必须在任何类型的解决方案中使用。我认为,即使使用无限多个生成器,也有可能工作,就空间而言,n是你能走多远的最大值,但就像我说的,在我的例子中,我不需要它。接受它是因为它更有效,尽管另一个同样有效。
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])