You need to pass on the results of the recursive call; each call returns a generator and you do have to iterate over that. The answer you linked to certainly loops over the recursive call too.
Add a for
loop over permutation_rec1()
and yield each resulting value on to the next caller:
def permutation_rec1(onelist, prelist):
if not onelist:
yield prelist
lenlist = len(onelist)
for i, oneitem in enumerate(onelist):
leftlist = [onelist[j] for j in range(lenlist) if j != i]
for res in permutation_rec1(leftlist, prelist + [oneitem]):
yield res
If you are using Python 3.3 or newer, you can use the new yield from
generator delegation syntax:
def permutation_rec1(onelist,prelist):
if not onelist:
yield prelist
lenlist = len(onelist)
for i, oneitem in enumerate(onelist):
leftlist = [onelist[j] for j in range(lenlist) if j != i]
yield from permutation_rec1(leftlist, prelist + [oneitem])