Problem of function calls from map()

Paul McGuire ptmcg at austin.rr._bogus_.com
Mon Aug 21 11:55:23 EDT 2006


"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





More information about the Python-list mailing list