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 = [] >>>

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

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

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 cameron.pulsf...@gmail.com (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

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

Re: Question about generators

2009-07-12 Thread Mensanator
On Jul 12, 2:11 pm, Cameron Pulsford cameron.pulsf...@gmail.com 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 = []    

Re: Question about generators

2009-07-12 Thread Vilya Harvey
2009/7/12 Cameron Pulsford cameron.pulsf...@gmail.com: 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

Re: Question about generators

2009-07-12 Thread Piet van Oostrum
Cameron Pulsford cameron.pulsf...@gmail.com (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

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 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 John Machin
On Jul 13, 11:24 am, Cameron Pulsford cameron.pulsf...@gmail.com 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

Re: Question about generators

2009-07-12 Thread David Robinow
On Sun, Jul 12, 2009 at 9:24 PM, Cameron Pulsfordcameron.pulsf...@gmail.com 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

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

Re: Question about generators

2009-07-12 Thread John Machin
On Jul 13, 1:17 pm, Cameron Pulsford cameron.pulsf...@gmail.com 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

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

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: #not functional.

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