-4

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

Michael Szczesny
  • 4,911
  • 5
  • 15
  • 32

3 Answers3

0

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)]
cigien
  • 57,834
  • 11
  • 73
  • 112
0

If you want to use regex, then:

import re
code = r'10001100'
output_list = re.findall(r'(0+|1+)', code)
Alexander Mashin
  • 3,892
  • 1
  • 9
  • 15
0

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']
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8