Splitting a string

Xavier Ho contact at xavierho.com
Fri Apr 2 06:36:37 EDT 2010


Oops, minor update:

>>> def split_number(string):
...     output = [string[0]]
...     for character in string[1:]:
...         if character.isdigit() != output[-1].isdigit():
...             if output[-1].isdigit() is True:
...                 output[-1] = int(output[-1])
...             output.append('')
...         output[-1] += character
...     return tuple(output)
...
>>> split_number('si_pos_99_rep_1_0.ita')
('si_pos_', 99, '_rep_', 1, '_', 0, '.ita')


Cheers,
Xav

On Fri, Apr 2, 2010 at 8:26 PM, Xavier Ho <contact at xavierho.com> wrote:

> Best I can come up with:
>
> >>> def split_number(string):
> ...     output = [string[0]]
> ...     for character in string[1:]:
> ...         if character.isdigit() != output[-1].isdigit():
> ...             output.append('')
> ...         output[-1] += character
> ...     return tuple(output)
> ...
> >>> split_number('si_pos_99_rep_1_0.ita')
>
> ('si_pos_', '99', '_rep_', '1', '_', '0', '.ita')
>
> Cheers,
> Xav
>
>
> On Fri, Apr 2, 2010 at 8:12 PM, Thomas Heller <theller at ctypes.org> wrote:
>
>> Maybe I'm just lazy, but what is the fastest way to convert a string
>> into a tuple containing character sequences and integer numbers, like
>> this:
>>
>>
>> 'si_pos_99_rep_1_0.ita'  -> ('si_pos_', 99, '_rep_', 1, '_', 0, '.ita')
>>
>> Thanks for ideas,
>> Thomas
>>
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100402/c7e7b257/attachment-0001.html>


More information about the Python-list mailing list