473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 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 1409
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

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

Similar topics

24
by: Mahesh Padmanabhan | last post by:
Hi, When list comprehension was added to the language, I had a lot of trouble understanding it but now that I am familiar with it, I am not sure how I programmed in Python without it. Now I...
9
by: Neuruss | last post by:
I have a doubt regarding list comprehensions: According to Mark Lutz in his book Learning Pyhon: "...there is currently a substantial performance advantage to the extra complexity in this case:...
23
by: Mike Meyer | last post by:
Ok, we've added list comprehensions to the language, and seen that they were good. We've added generator expressions to the language, and seen that they were good as well. I'm left a bit...
30
by: Steven Bethard | last post by:
George Sakkis wrote: > "Steven Bethard" <steven.bethard@gmail.com> wrote: >> Dict comprehensions were recently rejected: >> http://www.python.org/peps/pep-0274.html >> The reason, of course,...
6
by: Lonnie Princehouse | last post by:
List comprehensions appear to store their temporary result in a variable named "_" (or presumably "_", "_" etc for nested comprehensions) In other words, there are variables being put into the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.