471,310 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

Generators vs list comprehensions

Motoma
3,237 Expert 2GB
I would like to calculate the value of this series (for limited number of n)
s=2(1/1 - 1/3 + 1/5 -1/7 + 1/9 - 1/11+...)
Check it:
Expand|Select|Wrap|Line Numbers
  1. >>> sum([(((-1)**n)*(float(1)/(1+2*n))) for n in range(0, 8)])
  2. 0.75426795426795445
  3.  
If you need some help understanding this, let me know.
Oct 11 '07 #1
5 1286
bartonc
6,596 Expert 4TB
Check it:
Expand|Select|Wrap|Line Numbers
  1. >>> sum([(((-1)**n)*(float(1)/(1+2*n))) for n in range(0, 8)])
  2. 0.75426795426795445
  3.  
If you need some help understanding this, let me know.
Very nice! To increase efficiency and reduce syntactic clutter, use a generator instead of a list (I also dumped one set of parens on GPs):
Expand|Select|Wrap|Line Numbers
  1. >>> sum(((-1)**n)*(float(1)/(1+2*n)) for n in xrange(8))
  2. 0.75426795426795445
  3. >>> 
[EDIT: actually two generators, by using xrange() rather than range()]
Oct 11 '07 #2
Motoma
3,237 Expert 2GB
Very nice! To increase efficiency and reduce syntactic clutter, use a generator instead of a list (I also dumped one set of parens on GPs):
Expand|Select|Wrap|Line Numbers
  1. >>> sum(((-1)**n)*(float(1)/(1+2*n)) for n in xrange(8))
  2. 0.75426795426795445
  3. >>> 
[EDIT: actually two generators, by using xrange() rather than range()]
FANTASTIC!
This is the first time I have been introduced to generators!
I honestly learn something new every time I post in this forum.
Oct 11 '07 #3
bartonc
6,596 Expert 4TB
FANTASTIC!
This is the first time I have been introduced to generators!
I honestly learn something new every time I post in this forum.
From what I understand, generators are something to get used to.
They are efficient because they return values instead of storing them.
This means that you can't index into them nor print the entire contents; You must iterate over them.
They are required learning because Python 3.0 uses them in many places where lists were used in 2.5.
Oct 11 '07 #4
Motoma
3,237 Expert 2GB
From what I understand, generators are something to get used to.
They are efficient because they return values instead of storing them.
This means that you can't index into them nor print the entire contents; You must iterate over them.
They are required learning because Python 3.0 uses them in many places where lists were used in 2.5.
I've told you this before, but every time I sit down and work with Python, I love it more and more.

I sat down and did a little more reading on generators. When I was done, I ran over to my co-worker's cube in excitement to tell him about my discovery.

:D
Oct 11 '07 #5
bartonc
6,596 Expert 4TB
I've told you this before, but every time I sit down and work with Python, I love it more and more.

I sat down and did a little more reading on generators. When I was done, I ran over to my co-worker's cube in excitement to tell him about my discovery.

:D
Very cool. (sometimes I wish I had co-workers, for that very reason).

By the way, keep your eye out for split threads or you make work for the poor Site Moderator: lol ;)

[EDIT: Nooow I see your tag reflects an equal status...]
Oct 12 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

24 posts views Thread by Mahesh Padmanabhan | last post: by
9 posts views Thread by Neuruss | last post: by
30 posts views Thread by Steven Bethard | last post: by
6 posts views Thread by Lonnie Princehouse | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.