multi split function taking delimiter list

Paddy paddy3118 at netscape.net
Wed Nov 15 09:20:27 EST 2006


Paddy wrote:

> Paddy wrote:
>
> > 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.
> Arghhh!
> No colon!
> Forget the above please.
>
> - Pad.

With colon:

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

- Pad.




More information about the Python-list mailing list