443,846 Members | 1,862 Online
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

# need help on generator... (re)

 P: n/a 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 cs*************@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, Jul 18 '05 #1