我是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))