Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: two generators working in tandem

Reply
Thread Tools

Re: two generators working in tandem

 
 
Michael Spencer
Guest
Posts: n/a
 
      02-10-2006
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',
, ('A', 9), ('B', 1), ('B', 2), ('B', 3), ('B', 4), ('B', 5), ('B', 6), ('B',
7), ('B', , ('B', 9), ('C', 1), ('C', 2), ('C', 3), ('C', 4), ('C', 5), ('C',
6), ('C', 7), ('C', , ('C', 9)]
>>>



HTH, Michael


 
Reply With Quote
 
 
 
 
Anton Vredegoor
Guest
Posts: n/a
 
      02-12-2006

Michael Spencer wrote:

> 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)


Nice!

Here's a nonrecursive version. It creates a list of iterators that are
repeating their values just enough times to sychronize the nesting. I
wonder if 'ncycle' would be a useful generalization for itertools'
'cycle' function.

Anton

def ncycle(seq,n):
while True:
for x in seq:
for dummy in xrange(n):
yield x

def cross(*args):
p = 1
R = []
for arg in args[::-1]:
L = list(arg)
R.append(ncycle(L,p))
p *= len(L)
R.reverse()
for dummy in xrange(p):
yield [x.next() for x in R]

def test():
s1='a1','a2','a3','a4'
s2='b1','b2'
s3='c1','c2','c3'
for x in cross(s1,s2,s3):
print x

if __name__=='__main__':
test()

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Tandem stun problem lhicks@bppr.com Cisco 7 03-01-2006 08:27 PM
Use of AXIS on Tandem Ajay Java 0 10-16-2003 04:09 AM
Tandem DB Interface. Amitpython@aol.com Python 0 10-13-2003 04:23 PM
SPAM: Help finding C++ resources with Tandem/NSK E. Robert Tisdale C++ 0 07-15-2003 01:26 AM
Help finding C++ resources with Tandem/NSK Cindi C++ 1 07-15-2003 01:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57