multi split function taking delimiter list
paddy3118 at netscape.net
Wed Nov 15 07:58:19 CET 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.
Forget the above please.
More information about the Python-list