a splitting headache

John O'Hagan research at johnohagan.com
Fri Oct 16 03:39:19 CEST 2009


On Fri, 16 Oct 2009, Mensanator 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.

[...]
> 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']
> 
[...]

Or without resorting to re:

c.replace('0', ' ').split()
c.replace('1', ' ').split()

Three or four times faster, too!

Regards,

John



More information about the Python-list mailing list