stupid perl question

BJörn Lindqvist bjourne at gmail.com
Fri May 26 19:51:57 EDT 2006


> how can i split a string that contains white spaces and '_'
>
> any clue?

If the white spaces and the '_' should be applied equivalently on the
input and you can enumerate all white space characters, you could do
like this:

def split_helper(list, delims):
    if not delims:
        return list
    ch = delims[0]
    lst = []
    for item in list:
        lst += split_helper(item.split(ch), delims[1:])
    return lst

def split(str, delims):
    return split_helper([str], delims)

>>> split("foo_bar eh", "_ ")
['foo', 'bar', 'eh']

Though I bet someone will post a one-line solution in the next 30 minutes. :)

-- 
mvh Björn



More information about the Python-list mailing list