a splitting headache

Mensanator mensanator at aol.com
Thu Oct 15 21:18:09 EDT 2009


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.

But no, you can't do that.

>>> c = '0010000110'
>>> c.split('0')
['', '', '1', '', '', '', '11', '']

Ok, the consecutive delimiters appear as empty strings for
reasons unknown (except for the first one). Except when they
start or end the string in which case the first one is included.

Maybe there's a reason for this inconsistent behaviour but you
won't find it in the documentation.

And the re module doesn't help.

>>> f = '  1 2  3   4    '
>>> re.split(' ',f)
['', '', '1', '2', '', '3', '', '', '4', '', '', '', '']

OTOH, if my digits were seperated by whitespace, I could use
str.split(), which behaves differently (but not re.split()
because it requires a string argument).

>>> ' 1  11   111 11    '.split()
['1', '11', '111', '11']


That means I can use re to solve my problem after all.

>>> c = '0010000110'
>>> re.sub('0',' ',c).split()
['1', '11']
>>> re.sub('1',' ',c).split()
['00', '0000', '0']

Would it have been that difficult to show in the documentation
how to do this?



More information about the Python-list mailing list