Reputation: 29
I have a serie of digits that are string, for example : IN : '01110100001101001001110100'
I want to split as follow: OUT: ['01110100', '0', '01101001', '0','01110100]
We can notice that ninth '0' is a separator of list of digits that are binary.
Said otherwise : first eighth, sperator'0',next height, separator '0', next height, separator '0', etc
I know how to split using nth element (Split string every nth character?), but the issue here is a bit more complicated:there is a separator '0'
Thanks a lot for your help.
Kindest regards
Upvotes: 1
Views: 146
Reputation: 781503
You can use a regular expression with capture groups.
import re
instr = '01110100001101001001110100'
outlist = list(sum(re.findall(r'(\d{8})(\d)', instr), ()))
print(outlist)
re.findall()
returns a list of tuples, list(sum(..., ())
flattens it into a single list.
Upvotes: 1
Reputation: 10809
You just want to alternate between grabbing a slice of length 8, and then a slice of length 1, right?
def get_slices(string):
from itertools import islice, cycle
string_iter = iter(string)
slice_lens = cycle([8, 1])
while slc := "".join(islice(string_iter, next(slice_lens))):
yield slc
print(list(get_slices("abcdefghijklmnopqr")))
Output:
['abcdefgh', 'i', 'jklmnopq', 'r']
Upvotes: 1