split() off last substring
Noah
noah at noah.org
Mon Apr 15 00:28:08 EDT 2002
Python does not have a reverse split, but of course
you can still do it. The simplest way would be to split
the string into all the fields and then just take the last field.
a = 'aaa.bbb.ccc'
f = a.split('.') # Split does not split on '.' by default,
# so you have to tell it to split on '.'.
c = a[-1] # first field from the right. Negative indexes
# look weird to a newbie, but it's cool.
You could also do something like this:
a = 'aaa.bbb.ccc'
reverse (a)
c,b = a.split('.', 1)
reverse (c)
Writing the reverse() function (because Python doesn't
have one of those either :-) is left as an exercise to the reader...
Arguably, you might just write your own split routine
for the same amount of trouble and twice the speed.
Yours,
Noah
-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of bvdpoel at uniserve.com
Sent: Sunday, April 14, 2002 7:58 PM
To: python-list at python.org
Subject: split() off last substring
Can I tell split() to split off the LAST seqment? I guess I want
something like:
a="aaa.bbb.ccc"
b,c=a.rsplit(1)
print b,c
# "aaa.bbb", "ccc"
Thx.
--
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bvdpoel at uniserve.com
WWW: http://users.uniserve.com/~bvdpoel
More information about the Python-list
mailing list