I need to split ones and zeros in any binary representation like this.
code = 10001100
output_list = [1,000,11,00]
I couldnt find the pattern. and I am using python3.x
I need to split ones and zeros in any binary representation like this.
code = 10001100
output_list = [1,000,11,00]
I couldnt find the pattern. and I am using python3.x
You don't really need a regex for this problem. You can use groupby
from itertools
to do this:
import itertools
code = "10001100"
gs = [list(g) for _, g in itertools.groupby(code)]
If you want to use regex, then:
import re
code = r'10001100'
output_list = re.findall(r'(0+|1+)', code)
regex is not required. Here is pythonic way to do it:
code = '10001100'
output_list = []
interim_list = [code[i] + ',' if i != len(code)-1 and code[i] != code[i+1] else code[i] for i in range(len(code))]
output_list.append(''.join(interim_list))
print(output_list)
>>> print(output_list)
['1,000,11,00']