我正在尝试编写一个生成器函数来进行置换练习。但它不返回任何东西。但是如果我用 ''lis.append(new[k])'' 替换 ''yield new[k]'',那么我会得到正确的排列列表。我在产量方面做错了吗?
tup=(1,2,3) # tup can be any sequence
new=[[]]*(len(tup)+1) # memory; new[0]=[], new[1] will be length 1 permutation, etc.
lis=[] # the list of permutations
def repeat(k): # recursion
for i in tup:
if i in new[k-1]:
continue # permutation can't repeat
else: new[k]=new[k-1]+[i]
if k==len(tup):
yield new[k]
else:
repeat(k+1)
gen=repeat(1)
for i in gen:
print(i)