Problem of function calls from map()

Georg Brandl g.brandl-nospam at gmx.net
Tue Aug 22 06:31:16 EDT 2006


Paul McGuire wrote:
> "Dasn" <dasn at bluebottle.com> wrote in message
> news:mailman.9606.1156169593.27775.python-list at python.org...
>>
>> Hi, there.
>>
>> 'lines' is a large list of strings each of which is seperated by '\t'
>> >>> lines = ['bla\tbla\tblah', 'bh\tb\tb', ... ]
>>
>> I wanna split each string into a list. For speed, using map() instead
>> of 'for' loop.
> 
> Try this.  Not sure how it stacks up for speed, though.  (As others have
> suggested, if 'for' loop is giving you speed heartburn, use a list
> comprehension.)
> 
> In this case, splitUsing is called only once, to create the embedded
> function tmp.  tmp is the function that split will call once per list item,
> using whatever characters were specified in the call to splitUsing.
> 
> -- Paul
> 
> 
> 
> data = [
> "sldjflsdfj\tlsjdlj\tlkjsdlkfj",
> "lsdjflsjd\tlsjdlfdj\tlskjdflkj",
> "lskdjfl\tlskdjflj\tlskdlfkjsd",
> ]
> 
> def splitUsing(chars):
>     def tmp(s):
>         return s.split(chars)
>     return tmp
> 
> for d in map(splitUsing('\t'), data):
>     print d

And why is this better than

map(lambda t: t.split('\t'), data)

?

Georg



More information about the Python-list mailing list