Re: [Tutor] increment a counter inside generator

2013-03-14 Thread David Knupp
On Thu, 14 Mar 2013, Steven D'Aprano wrote: If you have some unknown, arbitrary iterable that doesn't support len(), then you can use the sum() trick: it = some_unknown_iterable() sum(1 for x in it) Yes, of course you are correct. This was my intention, but I chose an especially poorly contri

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Steven D'Aprano
On 14/03/13 12:13, David Knupp wrote: FWIW, if you're working with very large lists, but don't need to create the full list in memory, then a generator expression is usually preferred. To get the number of items a generator would return, you can use sum() like this: gen = (n for n in xrange(

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Steven D'Aprano
On 14/03/13 08:12, Abhishek Pratap wrote: import numpy as np count = 0 [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] File "", line 2 [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] ^ SyntaxError: invalid syntax [...] I

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Mark Lawrence
On 14/03/2013 01:13, David Knupp wrote: On Wed, 13 Mar 2013, Oscar Benjamin wrote: (it's not actually a generator by the way) As Oscar points out, you're not working with a generator expression. The syntactical difference between a list comprehension and a generator expression is subtle. List

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread David Knupp
On Wed, 13 Mar 2013, Oscar Benjamin wrote: (it's not actually a generator by the way) As Oscar points out, you're not working with a generator expression. The syntactical difference between a list comprehension and a generator expression is subtle. List comprehensions use square brackets, but

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Oscar Benjamin
On 13 March 2013 21:12, Abhishek Pratap wrote: > On Wed, Mar 13, 2013 at 2:02 PM, Oscar Benjamin > wrote: >> On 13 March 2013 19:50, Abhishek Pratap wrote: >>> Hey Guys >>> >>> I might be missing something obvious here. >>> >>> >>> import numpy as np >>> >>> count = 0 >>> [ count += 1 for num in

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Abhishek Pratap
On Wed, Mar 13, 2013 at 2:08 PM, Dave Angel wrote: > On 03/13/2013 03:50 PM, Abhishek Pratap wrote: >> >> Hey Guys >> >> I might be missing something obvious here. >> >> >> import numpy as np >> >> count = 0 >> [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] >> >> File "

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Abhishek Pratap
On Wed, Mar 13, 2013 at 2:02 PM, Oscar Benjamin wrote: > On 13 March 2013 19:50, Abhishek Pratap wrote: >> Hey Guys >> >> I might be missing something obvious here. >> >> >> import numpy as np >> >> count = 0 >> [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] >> >> File

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Dave Angel
On 03/13/2013 03:50 PM, Abhishek Pratap wrote: Hey Guys I might be missing something obvious here. import numpy as np count = 0 [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] File "", line 2 [ count += 1 for num in np.random.random_integers(1,100,20) if num >

Re: [Tutor] increment a counter inside generator

2013-03-13 Thread Oscar Benjamin
On 13 March 2013 19:50, Abhishek Pratap wrote: > Hey Guys > > I might be missing something obvious here. > > > import numpy as np > > count = 0 > [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] > > File "", line 2 > [ count += 1 for num in np.random.random_integers(1,

[Tutor] increment a counter inside generator

2013-03-13 Thread Abhishek Pratap
Hey Guys I might be missing something obvious here. import numpy as np count = 0 [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] File "", line 2 [ count += 1 for num in np.random.random_integers(1,100,20) if num > 20] ^ SyntaxError: invalid syntax A