0

I have this string:

string = 2p3g4p3y1g3w4w5w1w1y2w2g1p5g4g1r6g5r2r3r4r3b4b2b6r2y5y4y6w6y3p6b5b1b6p5p

I want to print it like this:

['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']

I know it is with split, but I don't know how to divide it in 6 lists

  • 1
    `split` splits on a delimiter; you just want to split on *position*. – chepner Jul 16 '20 at 15:46
  • 2
    Do you want 6 variable-length lists, or a variable number of 6-element lists, or is your string always exactly 72 characters long? – chepner Jul 16 '20 at 15:46
  • My string is always exactly 72 characters long – queenbegop777 Jul 16 '20 at 15:48
  • print("""['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']""" – Paul Collingwood Jul 16 '20 at 15:55

3 Answers3

1

There are three pieces:

  1. re.split, to break string into a list of character pairs
  2. filter, to get rid of the empty strings our use of re.split will produce
  3. 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)
chepner
  • 497,756
  • 71
  • 530
  • 681
1

Based upon dividing a list into equal size chunks Reference

def chunks(lst, n):
    """Yield successive n-sized chunks from lst."""
    for i in range(0, len(lst), n):
        yield lst[i:i + n]

Generating Output

string = '2p3g4p3y1g3w4w5w1w1y2w2g1p5g4g1r6g5r2r3r4r3b4b2b6r2y5y4y6w6y3p6b5b1b6p5p'

# Using chunks
for line in chunks(string, 12):                  # Chunk 12 elements per row
    s = ["'" + x + "'" for x in chunks(line, 2)] # chunks of two in quotes
    print(f'[{", ".join(s)}]')
  

Output

['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']

Simpler Alternative to f-string (pointed out by @user120242 in comments)

for line in chunks(string, 12):
  print(list(chunks(line, 2))) 
DarrylG
  • 16,732
  • 2
  • 17
  • 23
0

This could be done with nested lists of comprehension:

output = [[string[i+j*6:i+2+j*6] for i in range(0,12,2)] for j in range(0,int(len(string)/6),2)]

#Print each list separatly:
for x in output:
    print(x)

Output

 ['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']
Sebastien D
  • 4,369
  • 4
  • 18
  • 46