a splitting headache

Mensanator mensanator at aol.com
Sat Oct 17 06:05:59 CEST 2009


On Oct 16, 8:00�pm, Thomas <thom1... at gmail.com> wrote:
> On Oct 15, 9:18�pm, 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.
>
> > 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?
>
> PythonWin 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
> (Intel)] on win32.
> Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin'
> for further copyright information.>>> list('001010111100101')
>
> ['0', '0', '1', '0', '1', '0', '1', '1', '1', '1', '0', '0', '1', '0',
> '1']

Thanks, but what I wanted was
['00','1','0','1','0','1111','00','1','0' '1'].

>
>
>
> TC




More information about the Python-list mailing list