a splitting headache

Vlastimil Brom vlastimil.brom at gmail.com
Mon Oct 26 08:28:53 EDT 2009


2009/10/26 jhermann <Juergen.Hermann at 1und1.de>:
> On 16 Okt., 02:18, Mensanator <mensana... at aol.com> wrote:
>> All I wanted to do is split a binary number into two lists,
>> a list of blocks of consecutive ones and another list of
>> blocks of consecutive zeroes.
>
> Back to the OP's problem, the obvious (if you know the std lib) and
> easy solution is:
>
>>>> c = '001010111100101'
>>>> filter(None, re.split("(1+)", c))
> ['00', '1', '0', '1', '0', '1111', '00', '1', '0', '1']
>
> In production code, you compile the regex once, of course.
> --
> http://mail.python.org/mailman/listinfo/python-list
>

Maybe one can even forget the split function here entirely

>>> re.findall(r"0+|1+", '001010111100101')
['00', '1', '0', '1', '0', '1111', '00', '1', '0', '1']
>>>

vbr



More information about the Python-list mailing list