There are three pieces:
re.split
, to break string
into a list of character pairs
filter
, to get rid of the empty strings our use of re.split
will produce
grouper
, a function you can find in the itertools
documentation.
re.split
will use the regular expression (..)
to break string
into a list of empty strings separated by arbitrary two-character delimiters.
>>> re.split('(..)', string)
['', '2p', '', '3g', '', '4p', '', '3y', '', '1g', '', '3w', '', '4w', '', '5w', '', '1w', '', '1y', '', '2w', '', '2g', '', '1p', '', '5g', '', '4g', '', '1r', '', '6g', '', '5r', '', '2r', '', '3r', '', '4r', '', '3b', '', '4b', '', '2b', '', '6r', '', '2y', '', '5y', '', '4y', '', '6w', '', '6y', '', '3p', '', '6b', '', '5b', '', '1b', '', '6p', '', '5p', '']
filter(None, ...)
will remove all falsey values (i.e., empty strings) from that list. grouper
will break the stream of 36 pairs into a list of 6-pair tuples.
from itertools import zip_longest
import re
string = "2p3g4p3y1g3w4w5w1w1y2w2g1p5g4g1r6g5r2r3r4r3b4b2b6r2y5y4y6w6y3p6b5b1b6p5p"
# From https://docs.python.org/3/library/itertools.html
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks"
# grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
pairs = filter(None, re.split('(..)', string)
result = [list(x) for x in grouper(pairs, 6)]
print(result)