multi split function taking delimiter list
paddy3118 at netscape.net
Tue Nov 14 22:57:58 CET 2006
martinskou at gmail.com wrote:
> Hi, I'm looking for something like:
> multi_split( 'a:=b+c' , [':=','+'] )
> ['a', ':=', 'b', '+', 'c']
> whats the python way to achieve this, preferably without regexp?
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.
More information about the Python-list