need help on generator... (re)

Joh joh12005 at yahoo.fr
Mon Jan 24 17:06:49 EST 2005


hello,

thanks to all who replied to my post (2005-01-21) - (i can not post
inside the original thread as i get "Unable to retrieve message
csr7ep$3db$05$1 at news.t-online.com" from googlenews :(

> Do you mean:
> [1,2], [2,3], [3,4], [1,2,3], [2,3,4], [1,3,4]
> (E.g. all elements in the power set except the empty set, the sets with 
> one element and the sets with all elements.)
> Otherwise, please describe your desired sets in verbal - I cannot see 
> the point.

yes it was my wishes, but having the others empty, one and all
elements wasn't a big trouble, actually i wanted to understand more
generators.

> Here is an (untested) variant that accepts any iterable while trying to
> remain memory-efficient. This makes it necessary to shuffle the order of
> the output a bit.

> from itertools import tee, islice

> def gen(iterable, start, end):
>    it = iter(iterable)
>    while True:
>        it, a = tee(it)
>        a = tuple(islice(a, end-1))
>        for sz in xrange(start, len(a)+1):
>            yield a[:sz]
>        it.next()
>        
> if __name__ == "__main__":
>     print list(gen(range(1, 5), 2, 4))

please, this one looks interesting, could you explain a bit how it
works and why it "remain memory-efficient" ?

thanks,



More information about the Python-list mailing list