Re: Generator oddity

2009-05-01 Thread Andre Engels
On Fri, May 1, 2009 at 4:25 PM, Dave Angel wrote: > The problem is in it2, which is initialized only once.  Thus the second time > you're going through the c2 loop, it doesn't have any more values. > > If you need the indirection provided by those it1 and it2, you need to > postpone the function

Re: Generator oddity

2009-05-01 Thread Dave Angel
ops...@batnet.com wrote: I'm a little baffled by the inconsistency here. Anyone have any explanations? def gen(): ... yield 'a' ... yield 'b' ... yield 'c' ... [c1 + c2 for c1 in gen() for c2 in gen()] ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

Re: Generator oddity

2009-05-01 Thread opstad
D'oh! Many thanks for the explanation; I should have seen that myself. -- http://mail.python.org/mailman/listinfo/python-list

Re: Generator oddity

2009-05-01 Thread Duncan Booth
ops...@batnet.com wrote: it1 = gen() it2 = gen() list(c1 + c2 for c1 in it1 for c2 in it2) > ['aa', 'ab', 'ac'] > > Why does this last list only have three elements instead of nine? First time through the c1 in it1 loop you use up all of the it2 values. Second and third times thro

Re: Generator oddity

2009-05-01 Thread Steven D'Aprano
On Fri, 01 May 2009 04:58:16 -0700, opstad wrote: > I'm a little baffled by the inconsistency here. Anyone have any > explanations? > def gen(): > ... yield 'a' > ... yield 'b' > ... yield 'c' > ... [c1 + c2 for c1 in gen() for c2 in gen()] > ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', '

Re: Generator oddity

2009-05-01 Thread Pascal Chambon
ops...@batnet.com a écrit : I'm a little baffled by the inconsistency here. Anyone have any explanations? def gen(): ... yield 'a' ... yield 'b' ... yield 'c' ... [c1 + c2 for c1 in gen() for c2 in gen()] ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']

Generator oddity

2009-05-01 Thread opstad
I'm a little baffled by the inconsistency here. Anyone have any explanations? >>> def gen(): ... yield 'a' ... yield 'b' ... yield 'c' ... >>> [c1 + c2 for c1 in gen() for c2 in gen()] ['aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc'] >>> list(c1 + c2 for c1 in gen() for c2 in gen()) [