Search code examples
pythonregexsplitbinarypython-itertools

Split a binary to groups of 0s and 1s and get the boundary index


I have a binary number which I need to group into groups of 0s and 1s. Also I need to get the starting and ending index of each such newly formed group

For example, suppose the number is 1100111100

I need to group it as 11,00,1111,00

Now the starting and ending index of each group should be like

11 : (1,2) , 00 : (3,4) , 1111:(5,8) and 00: (9,10)

I am planning to use Python. I researched and found itertools can help but not sure which function to use in itertools.

Any help is deeply appreciated

Thanks


Solution

  • This can be done with regex like this in one line

    a = "1100111100"
    [' : '.join([i.group(),str((i.start()+1,i.end()))]) for i in re.finditer("0+|1+",a)]
    

    re.finditer

    Return an iterator yielding match objects over all non-overlapping matches for the RE pattern in string.

    meaning all unique hits are returned in an iterator

    Output

    ['11 : (1, 2)', '00 : (3, 4)', '1111 : (5, 8)', '00 : (9, 10)']