a splitting headache



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?
.



Relevant Pages

  • Re: Anyone heard of Bee Lisp?
    ... so I took a quick look at the documentation. ... CAR/CDR/CONS - seems to work on lists instead of cons cells. ... QUOTE - Prevents evaluation of the parameter. ... STRREAD - reads a string from the console ...
    (comp.lang.lisp)
  • TOC of Python Cookbook now online (was Re: author index for Python Cookbook 2?)
    ... Processing a String One Character at a Time ... Finding a File on the Python Search Path ... Constructing Lists with List Comprehensions ... Looping over Items and Their Indices in a Sequence ...
    (comp.lang.python)
  • ANN: MeObjects Library for Delphi
    ... object type small and powerful. ... the Object instance can use the ClassType method return the ... Especially for lists of pointers to dynamically allocated memory. ... {Summary Adds Ansi String and correspondent object to a list. ...
    (borland.public.delphi.thirdpartytools.general)
  • Filling a grid with Sharepoint data
    ... /// Retrieves a Lists web service proxy with default settings. ... string strComputerName = System.Net.Dns.GetHostName.ToLower; ... XmlDocument xmlDoc, ... XmlElement elementMethod, ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Can Dir handle different file types?
    ... Dim list As String ... Public Function ListFiles(Paths As String, Patterns As String, Optional ListCount As Long = 0) As String ... Dim pathIndex As Long, patternIndex As Long, listPosition As Long ... ' Accept lists that start with up to 2 delimiters ...
    (microsoft.public.vb.general.discussion)