Question

I have seen solutions to this problem is other problems, but not in Python. I was wondering, how do I insert a character in a string after every x amount characters? For example, a forward-slash after every fourth character:

Before:
AsQs7d4dJh2h

After:
AsQs/7d4d/Jh2h

I know in Python I would have to use slice notation. x = AsQs7d4dJh2h x = x[0:4]

but this only gives me the first instance, I would like to be able to do this on any string regardless of the length of that string

UPDATE NEW PROBLEM: What I am trying to do is separate the string in to card pairs, (2-card texas holdem), the problem is that the algorithm doesn't take into account the '10' card so separating into every 4 doesn't work, for instance this happens when faced with a 10 card in the string:

AsQs/10dA/h10h/2h
AsQs/10dA/h9h8/h
AsQs/10dA/h9h7/h
AsQs/10dA/h9h6/h
AsQs/10dA/h9h5/h
AsQs/10dA/h9h4/h
AsQs/10dA/h9h3/h
AsQs/10dA/h9h2/h
AsQs/10dA/h8h7/h
AsQs/10dA/h8h6/h

So what I need help with is how do I insert a forward slash or extract all the 2 card hands (in this instance 3 hands) from the string regardless if it has 1 or multiple '10' cards in the string?

Complete algorithm:

import itertools

strOutput = ""
lstMaster = ['As', 'Ks', 'Qs', 'Js', '10s', '9s', '8s', '7s', '6s', '5s', '4s', '3s', '2s',\
                 'Ad', 'Kd', 'Qd', 'Jd', '10d', '9d', '8d', '7d', '6d', '5d', '4d', '3d', '2d',\
                 'Ac', 'Kc', 'Qc', 'Jc', '10c', '9c', '8c', '7c', '6c', '5c', '4c', '3c', '2c',\
                 'Ah', 'Kh', 'Qh', 'Jh', '10h', '9h', '8h', '7h', '6h', '5h', '4h', '3h', '2h']

tupMasterEdited = itertools.combinations(lstMaster, 6)
lstMasterEdited = list(tupMasterEdited)

for combo in lstMasterEdited:
    combo = str(combo).replace("(", "").replace(")", "").replace(" ", "").replace(",", "").replace("'", "")
    combo = '/'.join([combo[i:i+4] for i in range(0, len(combo), 4)])
    print(combo)
Was it helpful?

Solution 3

This can be solve with a replacement:

 import re

 re.sub(r'((?:(?=(10|.))\2){4})(?!$)', r'\1/', 'AsQs10dAh10h2h')

(?=(10|.))\2 emulates an atomic group (a feature that is not available in the re module) and stands for (?>10|.). This uses the fact that the content of a lookahead is atomic.

(?!$) is a negative lookahead and means not followed by the end of the string

OTHER TIPS

Probably not the best solution:

'/'.join([str[i:i+4] for i in range(0, len(str), 4)])

>>> str = "AsQs7d4dJh2h"
>>> '/'.join([str[i:i+4] for i in range(0, len(str), 4)])
'AsQs/7d4d/Jh2h'
>>> str = "sdfjsdhjfkbsdajka"
>>> '/'.join([str[i:i+4] for i in range(0, len(str), 4)])
'sdfj/sdhj/fkbs/dajk/a'

If regexes is an option:

>>> re.sub(r'(.{4})(?=.)', r'\1/', 'AsQs7d4dJh2h')
'AsQs/7d4d/Jh2h'

Here's a general solution forxnumber of characters. Thechargument can be one or several characters and there doesn't need to be an even multiple ofxcharacters in the string.

from itertools import izip_longest

def insert_every(x, ch, str):
    return ch.join(''.join(chars)
                   for chars in izip_longest(*([iter(str)]*x), fillvalue=''))

print insert_every(4, '/', 'AsQs7d4dJh2h')    # AsQs/7d4d/Jh2h
print insert_every(4, '+-', 'AsQs7d4dJh2hZ')  # AsQs+-7d4d+-Jh2h+-Z
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top