multi split function taking delimiter list

Paddy paddy3118 at netscape.net
Tue Nov 14 16:57:58 EST 2006


martinskou at gmail.com wrote:

> Hi, I'm looking for something like:
>
> multi_split( 'a:=b+c' , [':=','+'] )
>
> returning:
> ['a', ':=', 'b', '+', 'c']
>
> whats the python way to achieve this, preferably without regexp?
>
> Thanks.
>
> Martin

I resisted my urge to use a regexp and came up with this:

>>> from itertools import groupby
>>> s = 'apple=blue+cart'
>>> [''.join(g) for k,g in groupby(s, lambda x: x in '=+')]
['apple', '=', 'blue', '+', 'cart']
>>>

For me, the regexp solution would have been clearer, but I need to
stretch my itertools skills.

- Paddy.




More information about the Python-list mailing list