Python 递归生成器

Python Recursive Generator

我正在尝试制作一个递归生成器,但我显然遗漏了一些东西。基本上我有一个直方图特定维度的正值和负值列表,并且想要获得正值和负值的每个组合。 例如:

input = [[-1,2],[-3,4],[-5,6]]

我需要的结果是(虽然我希望它作为生成器而不是列表,但你明白了):

output = [[-1, -3, -5],[-1, -3, 6],[-1, 4, -5],[-1, 4, 6],[2, -3, -5],[2, -3, 6],[2, 4, -5],[2, 4, 6]]

我已经设法完成它,但只是通过将项目附加到我的递归内部的全局列表,但我真的希望我可以用生成器来完成它并迭代我的函数,因为这样就可以了IMO 干净多了。

这是我现在拥有的:

def combs(idx,array,current=[]):
    for item in array[idx]:
        current_copy = current[:]
        current_copy.append(item)
        if idx + 1 < len(array):
            combs(idx+1,array,current_copy)
        else:
            print current_copy
            # yield current_copy

这将打印出我想要的内容,但是当我尝试将打印更改为 yield 并在函数上循环时,它不起作用。 (例如)

for item in combs(0,input,[]):
    print item

如果您使用的是 Python 3.3 或更新版本,您需要 yield from 语句:

def combs(idx,array,current=[]):
    for item in array[idx]:
        current_copy = current[:]
        current_copy.append(item)
        if idx + 1 < len(array):
            yield from combs(idx+1,array,current_copy)
        else:
            yield current_copy

在 Python 2.7 上,您可以将 yield from 扩展为一个循环:

def combs(idx,array,current=[]):
    for item in array[idx]:
        current_copy = current[:]
        current_copy.append(item)
        if idx + 1 < len(array):
            for i in combs(idx+1,array,current_copy):
                yield i
        else:
            yield current_copy

此任务的另一个选项是 itertools.product 函数:

>>> from itertools import product
>>> input = [[-1,2],[-3,4],[-5,6]]
>>> list(product(*input))
[(-1, -3, -5), (-1, -3, 6), (-1, 4, -5), (-1, 4, 6), (2, -3, -5), (2, -3, 6), (2, 4, -5), (2, 4, 6)]