how to convert a function into generator?

Schüle Daniel uval at rz.uni-karlsruhe.de
Wed Dec 6 11:33:27 EST 2006


Hello,

I came up with this algorithm to generate all permutations
it's not the best one, but it's easy enough

# lst = list with objects
def permute3(lst):
     tmp = []
     lenlst = len(lst)
     def permute(perm, level):
         if level == 1:
             tmp.append(perm)
             return
         for i in lst:
             if i not in perm:
                 permute(perm + (i,), level - 1)
     for item in lst:
         permute((item,), lenlst)
     return tuple(tmp)

now I want to make a generator from it
the idea is to get each time a new permutation
I don't really understand how to handle the case
when my function has an inner function which has
a yield statement ..
I would say that the inner function becomes an generator
and stops&yields the value on each yield .. but in reality
I want to propogate these values to the caller of the outer function
I hope you got the idea of what I mean
the code is the sketch of the idea

def permute3gen(lst):
     lenlst = len(lst)
     def permute(perm, level):
         if level == 1:
             yield perm
             return	# not sure return without a value is allowed, 
theoretically it could be replaces with if/else block
         for i in lst:
             if i not in perm:
                 permute(perm + (i,), level - 1)
     for item in lst:
         yield permute((item,), lenlst) # makes generator from the outer 
function too


this is what I get

In [67]: reload permute
-------> reload(permute)
Out[67]: <module 'permute' from 'permute.pyc'>

In [68]: p =  permute.permute3gen(["a","b","c","d"])

In [69]: p
Out[69]: <generator object at 0x2af3a44795f0>

In [70]: x = p.next()

In [71]: y = p.next()

In [72]: x
Out[72]: <generator object at 0x2af3a448e830>

In [73]: y
Out[73]: <generator object at 0x2af3a448e878>

In [74]: x.next()
---------------------------------------------------------------------------
<type 'exceptions.StopIteration'>         Traceback (most recent call last)

/pool/PROG/python/permute/<ipython console> in <module>()

<type 'exceptions.StopIteration'>:

I don't understand why the generator x raises StopIteration exception
I would expect that x.next() would call
permute(("a",), 4) and would stop at "abcd"

thanks in advance

regards, Daniel



More information about the Python-list mailing list