On Fri, Jun 5, 2009 at 6:18 AM, Norman Khine <nor...@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'] > > I've tried: > >>> dir = '/expert/forum' > >>> dir.split('/') > ['', 'expert', 'forum'] > >>> dir.replace("/expert","") > '/forum' > >>> dir = '/expert/forum/expert' > >>> dir.replace("/expert","") > '/forum' will it always begin with /? and is there any reason you want to retain the /? because this gets them without the / >>> dir.split('/')[1:] ['expert', 'forum', 'expert', 'expert'] I think you might be able to use a list comprehension. Otherwise you could do this: >>> dir.replace('/',' /').split() ['/expert', '/forum', '/expert', '/expert'] HTH, Wayne
_______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor