[BangPypers] which one is the best pythonic way .
Navin Kabra
navin.kabra at gmail.com
Tue Feb 9 06:14:42 CET 2010
On Tue, Feb 9, 2010 at 10:22 AM, Srinivas Reddy Thatiparthy <
srinivas_thatiparthy at akebonosoft.com> wrote:
> 1.sum([i for i in range(1000) if i%3==0 or i%5==0])
>
Slightly better would be:
sum((i for i in range(1000) if i%3==0 or i%5==0))
> 2.gen=(i for i in range(1000))
> sum([i for i in gen if i%3==0 or i%5==0])
>
What I gave above is a better way of doing this
> 3.sum(filter(lambda a:a%3==0 or a%5==0,range(1000)))
>
avoid lambda's whenever possible. they are difficult to understand, and can
be avoided in most cases. Even Guido dislikes them.
> 4.def generator(m):
> count=0
> while count<m:
> if count%3==0 or count%5==0:
> yield count
> count+=1
> sum([i for i in generator(1000)])
>
Overkill.
More information about the BangPypers
mailing list