[Tutor] split or replace
W W
srilyk at gmail.com
Fri Jun 5 13:32:17 CEST 2009
On Fri, Jun 5, 2009 at 6: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']
>
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090605/ab33be43/attachment.htm>
More information about the Tutor
mailing list