Re: Question about generators

2021-03-05 Thread Frank Millman
On 2021-03-06 8:21 AM, Frank Millman wrote: Hi all This is purely academic, but I would like to understand the following - >>> >>> a = [('x', 'y')] >>> >>> s = [] >>> for b, c in a: ...   s.append((b, c)) ... >>> s [('x', 'y')] This is what I expected. >>> >>> s = [] >>> s.append(((

Re: Question about generators

2021-03-05 Thread Ming
On Sat, Mar 06, 2021 at 08:21:47AM +0200, Frank Millman wrote: > [...] > I understand the concept that a generator does not return a value until you > call next() on it, but I have not grasped the essential difference between > the above two constructions. > > TIA for any insights. > > Frank Mill

Re: Question about generators

2021-03-05 Thread Alan Bawden
>>> >>> s = [] >>> s.append(((b, c) for b, c in a)) >>> s [ at 0x019FC3F863C0>] >>> TIA for any insights. Replace "append" above with "extend" and observe the results. Then ponder the difference between append and extend. I suspect that the heart of your confusion actua

Question about generators

2021-03-05 Thread Frank Millman
Hi all This is purely academic, but I would like to understand the following - >>> >>> a = [('x', 'y')] >>> >>> s = [] >>> for b, c in a: ... s.append((b, c)) ... >>> s [('x', 'y')] This is what I expected. >>> >>> s = [] >>> s.append(((b, c) for b, c in a)) >>> s [ at 0x019FC3F863C0>]

Re: Question about generators

2009-07-13 Thread Piet van Oostrum
> Cameron Pulsford (CP) wrote: >CP> I read it on the haskell site in their sieves/prime wheel section, >CP> I guess I misunderstood something. (east to do over there...) I did >CP> verify it against established list of primes and other generators >CP> I've written that use more normal methods

Re: Question about generators

2009-07-12 Thread John Machin
On Jul 13, 1:17 pm, Cameron Pulsford wrote: > I read it on the haskell site in their sieves/prime wheel section, I   > guess I misunderstood something. (east to do over there...) I did   > verify it against established list of primes and other generators I've   > written that use more normal metho

Re: Question about generators

2009-07-12 Thread Cameron Pulsford
I read it on the haskell site in their sieves/prime wheel section, I guess I misunderstood something. (east to do over there...) I did verify it against established list of primes and other generators I've written that use more normal methods, but I only hand verified it. It is at least int

Re: Question about generators

2009-07-12 Thread David Robinow
On Sun, Jul 12, 2009 at 9:24 PM, Cameron Pulsford wrote: > As far as the primes generator, it does not generate any non-primes. All > primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) where is x is > [1, 2, ..., n]. The only time it doesn't generate a prime is when x + (1 or > 5) % 5 ==

Re: Question about generators

2009-07-12 Thread John Machin
On Jul 13, 11:24 am, Cameron Pulsford wrote: > As far as the primes generator, it does not generate any non-primes.   > All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5)   > where is x is [1, 2, ..., n]. The only time it doesn't generate a   > prime is when x + (1 or 5) % 5 == 0.

Re: Question about generators

2009-07-12 Thread Cameron Pulsford
itertools.chain() did it, thanks! As far as the primes generator, it does not generate any non-primes. All primes (except 2, 3 and 5) are in the form (6*x + 1, 6*x + 5) where is x is [1, 2, ..., n]. The only time it doesn't generate a prime is when x + (1 or 5) % 5 == 0. Which is what that

Re: Question about generators

2009-07-12 Thread Terry Reedy
Cameron Pulsford wrote: When you start a new thread, you should start a new thread and not piggyback on an existing thread. -- http://mail.python.org/mailman/listinfo/python-list

Re: Question about generators

2009-07-12 Thread Piet van Oostrum
> Cameron Pulsford (CP) wrote: >CP> Hey everyone, I have this small piece of code that simply finds the >CP> factors of a number. Others have already given you advice to add the [2, 3, 5] to the iterator (of which the primes.extend([2,3,5]) will not work). Please allow me to make some other

Re: Question about generators

2009-07-12 Thread Vilya Harvey
2009/7/12 Cameron Pulsford : > My question is, is it possible to combine those two loops? The primes > generator I wrote finds all primes up to n, except for 2, 3 and 5, so I must > check those explicitly. Is there anyway to concatenate the hard coded list > of [2,3,5] and the generator I wrote so

Re: Question about generators

2009-07-12 Thread Mensanator
On Jul 12, 2:11 pm, Cameron Pulsford wrote: > Hey everyone, I have this small piece of code that simply finds the   > factors of a number. > > import sys > > def factor(n): >      primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! > = 0) > >      factors = [] > >      for i in [2,

Question about generators

2009-07-12 Thread Cameron Pulsford
Hey everyone, I have this small piece of code that simply finds the factors of a number. import sys def factor(n): primes = (6*i+j for i in xrange(1, n) for j in [1, 5] if (i+j)%5 ! = 0) factors = [] for i in [2, 3, 5]: while n % i == 0: n /= i f

Re: Question about generators.

2006-01-30 Thread kiwwisk
Thanks for reply :) I'm little sceptic about this code: >>> a = gen1() >>> a.gi_frame.f_code.co_name 'gen1' will it be compatible with Python 2.3, 2.4, 2.5+ and future versions? Seems very internal and subject of future change. A. -- http://mail.python.org/mailman/listinfo/python-list

Re: Question about generators.

2006-01-30 Thread Paul McGuire
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Hi, > Is there a way for created generators to determine what function > created them? > Like for objects and classes function 'isinstance', > > e.g.: > > def gen1( ): >yield 1 > > a = gen1( ) > > if isinstance( a, gen1 ) == True: #n

Question about generators.

2006-01-30 Thread kiwwisk
Hi, Is there a way for created generators to determine what function created them? Like for objects and classes function 'isinstance', e.g.: def gen1( ): yield 1 a = gen1( ) if isinstance( a, gen1 ) == True: #not functional. ... Thanks for reply, Andrej -- http://mail.python.org