練習用の順列用のジェネレーター関数を書こうとしています。しかし、それは何も返しません。しかし、´´yield new [k]´´を´´lis.append(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)