Splitting a string
Patrick Maupin
pmaupin at gmail.com
Fri Apr 2 18:53:07 EDT 2010
On Apr 2, 4:32 pm, Peter Otten <__pete... at web.de> wrote:
> _split = re.compile(r"(\d+)").split
> def split(s):
> if not s:
> return ()
> parts = _split(s)
> parts[1::2] = map(int, parts[1::2])
> if parts[-1] == "":
> del parts[-1]
> if parts[0] == "":
> del parts[0]
> return tuple(parts)
>
That's certainly faster than a list comprehension (at least on long
lists), but it might be a little obscure why the "if not s:" is
needed, so unless Thomas has a really long result list, he might want
to just keep the list comprehension, which is (IMO) very readable.
Alternatively, this is halfway between the previous example and the
list comprehension:
_split = re.compile(r"(\d+)").split
def split(s):
parts = _split(s)
parts[1::2] = map(int, parts[1::2])
for index in (-1, 0):
if parts and parts[index] == "":
del parts[index]
return tuple(parts)
BTW, I just remembered that, although I have often used the fact that
split returns alternating non-match/match/.../match/non-match in the
past, the last time I did this particular task (of splitting out
digits from a string), I didn't make use of that fact. But I wasn't
expecting very many splits for this case. FWIW, here's a class I
wrote that does this to a string for the express purpose of making
sorts work better:
http://code.google.com/p/pyeda/source/browse/trunk/kipy/kipy/utility/istring.py
Regards,
Pat
More information about the Python-list
mailing list