[BangPypers] which one is the best pythonic way .
I have written four solutions to a problem(self explanatory) ,out of them ,which one is the pythonic way of doing and is there any other ways of solving it? 1.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]) 3.sum(filter(lambda a:a%3==0 or a%5==0,range(1000))) 4.def generator(m): count=0 while countm: if count%3==0 or count%5==0: yield count count+=1 sum([i for i in generator(1000)]) Regards, ~ Srini T ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
On Tue, Feb 9, 2010 at 10:22 AM, Srinivas Reddy Thatiparthy srinivas_thatipar...@akebonosoft.com wrote: I have written four solutions to a problem(self explanatory) ,out of them ,which one is the pythonic way of doing and is there any other ways of solving it? 1.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]) 3.sum(filter(lambda a:a%3==0 or a%5==0,range(1000))) 4.def generator(m): count=0 while countm: if count%3==0 or count%5==0: yield count count+=1 sum([i for i in generator(1000)]) #1. You can even remove the square brackets. sum(i for i in range(1000) if i%3==0 or i%5==0) ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
On Tue, Feb 9, 2010 at 10:22 AM, Srinivas Reddy Thatiparthy srinivas_thatipar...@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 countm: if count%3==0 or count%5==0: yield count count+=1 sum([i for i in generator(1000)]) Overkill. ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
Thanks for the replies and I avoid using lambdas.. Btw,Shall I avoid using filter and map ? Because what ever filter and map do,I could seem to do the same with Listcomprehensions.. Is there any situation in which they fare better than list comprehensions? Regards, ~ Srini T ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
filter, map (reduce, any, all) come from the world of Functional Programming. For that matter list comprehension was borrowed from FP (haskell). I wont attempt to compare them as they cater to different needs. Yes, they overlap in certain areas but I'd like to think of them as seperate assets suited for different needs. For simple stuff, List Comprehension offers superior readability but it can get too much too fast if you start chaining a lot. Do not overlook the power of filter, map as they _are_ valuable tools in your everyday programming. jeff PS: If it interests you, read about MapReduce that Google use to come up with lightening fast search results. yes. mapreduce = map + reduce. simple, effective, elegant. On Tue, Feb 9, 2010 at 10:52 AM, Srinivas Reddy Thatiparthy srinivas_thatipar...@akebonosoft.com wrote: Thanks for the replies and I avoid using lambdas.. Btw,Shall I avoid using filter and map ? Because what ever filter and map do,I could seem to do the same with Listcomprehensions.. Is there any situation in which they fare better than list comprehensions? Regards, ~ Srini T ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
On 02/09/2010 10:37 AM, Anand Chitipothu wrote: On Tue, Feb 9, 2010 at 10:22 AM, Srinivas Reddy Thatiparthy [...snip...] #1. You can even remove the square brackets. sum(i for i in range(1000) if i%3==0 or i%5==0) Yes, you can but that doesn't necessarily mean it is better. Interestingly, something similar was discussed at comp.lang.python: http://groups.google.com/group/comp.lang.python/browse_thread/thread/7897bf3fd2298332/26247987f84cc484 cheers, - steve -- random non tech spiel: http://lonetwin.blogspot.com/ tech randomness: http://lonehacks.blogspot.com/ what i'm stumbling into: http://lonetwin.stumbleupon.com/ ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
On Tue, Feb 9, 2010 at 10:52 AM, Srinivas Reddy Thatiparthy srinivas_thatipar...@akebonosoft.com wrote: Thanks for the replies and I avoid using lambdas.. Btw,Shall I avoid using filter and map ? Because what ever filter and map do,I could seem to do the same with Listcomprehensions.. Is there any situation in which they fare better than list comprehensions? map, filter, reduce at al are orders slower when compared to list comprehensions, cuz each invocation of these cost one function call, whereas list comprehensions are optimized in the language. For example, def f1(): [i for i in range(1000) if i % 3 == 0] ... def f2(): filter(lambda x: x%3==0, range(1000)) And using timeit, mytimeit.Timeit(f1, number=1) '211.47 usec/pass' mytimeit.Timeit(f2, number=1) '319.08 usec/pass' The only advantage of map, filter etc over list comprehensions is that they are sometimes concise over the equivalent list comprehensions, when combined with already defined functions. For example, [ x for x in range(1000) if (x%3==0) or (x%5==0)] def f(x): (x%3==) or (y %5==0) map(f, range(1000)) But again, keep in mind that these are much slower when compared to the list comprehension or generator equivalent. There are a few cases when a map function looks more elegant when compared to the equivalent list comp. For example, let us say you want to add together two lists. l1=range(5, 10) l2=range(10, 15) map(lambda x,y: x+y, l1, l2) [15, 17, 19, 21, 23] There is no direct way to do this in list comp except taking the help of zip. [a+b for a,b in zip(l1,l2)] [15, 17, 19, 21, 23] But practicality beats purity. Given a choice, I will always use the latter instead of the former, irrespective of the elegance, which is questionable and subjective anyway :) Regards, ~ Srini T ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers -- --Anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] which one is the best pythonic way .
On Tue, Feb 9, 2010 at 1:03 PM, Dhananjay Nene dhananjay.n...@gmail.comwrote: Anand Balachandran Pillai wrote: On Tue, Feb 9, 2010 at 10:52 AM, Srinivas Reddy Thatiparthy srinivas_thatipar...@akebonosoft.com wrote: Thanks for the replies and I avoid using lambdas.. Btw,Shall I avoid using filter and map ? Because what ever filter and map do,I could seem to do the same with Listcomprehensions.. Is there any situation in which they fare better than list comprehensions? map, filter, reduce at al are orders slower when compared to list comprehensions, cuz each invocation of these cost one function call, whereas list comprehensions are optimized in the language. For example, def f1(): [i for i in range(1000) if i % 3 == 0] ... def f2(): filter(lambda x: x%3==0, range(1000)) And using timeit, mytimeit.Timeit(f1, number=1) '211.47 usec/pass' mytimeit.Timeit(f2, number=1) '319.08 usec/pass' The only advantage of map, filter etc over list comprehensions is that they are sometimes concise over the equivalent list comprehensions, when combined with already defined functions. For example, [ x for x in range(1000) if (x%3==0) or (x%5==0)] def f(x): (x%3==) or (y %5==0) map(f, range(1000)) But again, keep in mind that these are much slower when compared to the list comprehension or generator equivalent. There are a few cases when a map function looks more elegant when compared to the equivalent list comp. For example, let us say you want to add together two lists. l1=range(5, 10) l2=range(10, 15) map(lambda x,y: x+y, l1, l2) [15, 17, 19, 21, 23] There is no direct way to do this in list comp except taking the help of zip. [a+b for a,b in zip(l1,l2)] [15, 17, 19, 21, 23] But practicality beats purity. Given a choice, I will always use the latter instead of the former, irrespective of the elegance, which is questionable and subjective anyway :) Interestingly in my computations the second approach (zip) is about 35% slower (as measured over 5 runs of 1M iterations each) than the first one (map/lambda). I believe the performance argument should be a bit nuanced. The earlier example you demonstrated with list comprehension is faster due to the fact that the if condition (and only the if condition alone - which is the equivalent of filter) is inlined vs being a function call. For other scenarios not involving a filter, I suspect performance could vary either way. I would rephrase the earlier statement as follows : map, filter, reduce at al are *is** *orders slower when compared to list comprehensions, cuz each invocation of these cost one function call, whereas list comprehensions are optimized in the language. I was incorrect above .. there are clearly other situations where the benefits of inlining occur apart from the filter function . The statement rephrasing is incorrect Regards, ~ Srini T ___ BangPypers mailing list bangpyp...@python.orghttp://mail.python.org/mailman/listinfo/bangpypers -- blog: http://blog.dhananjaynene.com twitter: http://twitter.com/dnene http://twitter.com/_pythonic ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers