473,386 Members | 1,969 Online

# Generators vs list comprehensions

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