[Tutor] map, filter and lambda functions

Hans Nowak hnowak at cuci.nl
Thu Aug 23 04:57:11 EDT 2001


>===== Original Message From "Kerim Borchaev" <warkid at storm.ru> =====
>Thursday, August 23, 2001, 9:23:50 AM, you wrote:
>
>RR> checklist = string.split(filetext, '\n')
>RR> checklist = filter(None, checklist)
>RR> checklist = map(string.strip, checklist)
>RR> checklist = map(string.upper, checklist)
>
>RR> If checklist is rather long, the last two may be written mor efficiently
>RR> as:
>
>RR> checklist = map(lambda x:string.upper(string.strip(x)), checklist)
>
>About efficiency - running the script below i get this:
>
>2.01
>1.32
>2.01
>1.32
>
>no luck for lambda...

On my box, these two are faster:

 print "trial 5:",
 def popo(x):
     return x.strip().upper()
 start = time.clock()
 for i in range(num_iter):
     checklist=l
     checklist = map(popo, checklist)
 stend = time.clock()
 print "%.2f"%(stend-start)

 print "trial 6:",
 start = time.clock()
 for i in range(num_iter):
     checklist=l
     checklist = [x.strip().upper() for x in checklist]
 stend = time.clock()
 print "%.2f"%(stend-start)

I'm on an overloaded NT box right now, and the results vary horribly for each 
run, but 5 and 6 are consistently faster than the rest.

Mzzl,

--Hans Nowak





More information about the Python-list mailing list