[Tutor] split or replace

Kent Johnson kent37 at tds.net
Fri Jun 5 13:47:53 CEST 2009


On Fri, Jun 5, 2009 at 7:18 AM, Norman Khine<norman at khine.net> wrote:
> Hello,
> What is the way to group each value so that I get a list, from a string like:
>
> dir = '/expert/forum/expert/expert'
> list = ['/expert', '/forum', '/expert', '/expert']

Here is one way using re.split():
In [28]: import re
In [29]: [ x for x in re.split(r'(/[^/]+)', dir) if x ]
Out[29]: ['/expert', '/forum', '/expert', '/expert']

Kent


More information about the Tutor mailing list