john peter wrote: > I'd like to write two generators: one is a min to max sequence number > generator that > rolls over to min again once the max is reached. the other is a generator > that cycles > through N (say, 12) labels. currently, i'm using these generators in nested > loops like > this: > > seq_numbers = genSeqNum(min,max) > for label in genLabel(): > for some_date in genDate(init): > for i in xrange(0, LIMIT): > print label, some_date, i, seq_numbers.next() > > The problem I'm trying to solve is this: > when the seq_numbers generator rolls over, the label generator must be > advanced > to the next one "in tandem". does anyone has any suggestion? thanks for any > hepl! > > > --------------------------------- > Yahoo! Mail > Use Photomail to share photos without annoying attachments. > This returns an iterator that 'nests' an arbitrary number of sequences (odometer-style).
def nest(*sequences): def _nest(outer, inner): for outer_item in outer: if not isinstance(outer_item, tuple): outer_item = (outer_item,) for inner_item in inner: yield outer_item + (inner_item,) return reduce(_nest, sequences) So, for your use case: >>> seq_numbers = range(1,10) >>> labels = ["A","B","C"] >>> list(nest(labels, seq_numbers)) [('A', 1), ('A', 2), ('A', 3), ('A', 4), ('A', 5), ('A', 6), ('A', 7), ('A', 8), ('A', 9), ('B', 1), ('B', 2), ('B', 3), ('B', 4), ('B', 5), ('B', 6), ('B', 7), ('B', 8), ('B', 9), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5), ('C', 6), ('C', 7), ('C', 8), ('C', 9)] >>> HTH, Michael -- http://mail.python.org/mailman/listinfo/python-list