Python生成器问题:不生成任何值

时间:2018-11-05 21:16:39

标签: python generator permutation

我是python的新手,我试图了解generator的工作原理。显然,我的代码不正确。有人可以帮我吗?

def permutation_backtrack(elements):
    n = len(elements)
    return backtrack(elements, 0, n-1)

def backtrack(elements, l, r):
    if l == r:
        yield elements
    else:
        for i in range(l,r+1,1):
            elements[l], elements[i] = elements[i], elements[l]
            backtrack(elements, l+1, r)
            elements[l], elements[i] = elements[i], elements[l]

backtrack_generator = permutation_backtrack([1,2,3])
print(list(backtrack_generator))

0 个答案:

没有答案