Splitting a string

Bearophile bearophileHUGS at lycos.com
Fri Apr 2 11:41:28 EDT 2010


I don't know how fast this is (Python 2.x):

>>> from itertools import groupby
>>> t = 'si_pos_99_rep_1_0.ita'
>>> tuple(int("".join(g)) if h else "".join(g) for h,g in groupby(t, str.isdigit))
('si_pos_', 99, '_rep_', 1, '_', 0, '.ita')

It doesn't work with unicode strings.

Bye,
bearophile



More information about the Python-list mailing list