Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python/Panda string split - keeping the splitter (separator)

I am trying to split a character string into block of known substring (sub-character). I believe I can achieve this if I am able to keep the separator in the output.

e.g:

re.split('LBT', 'HLHLBTS')
['HLH', 'S'] #output
['HLH', 'LBT', 'S'] #Needed output

Eventual final output that I am looking for:

['H', 'HL', 'HLH', 'HLHLBT','HLHLBTS'] # Can be achieved if I have above

I have tried this and it gets me the end result but I have simply brute forced it this particular situation

seq = re.split('', 'HLHLBTS')
seqout = []
sout = []
s = ''
count = 0
cond = 'run'
for i in range(len(seq)):
    if count == 2:
        cond = 'run'
    if cond == 'skip':
        count = count + 1
        continue
    if ((seq[i] == 'L') & (i < len(seq) - 4)):
        if ((seq[i+1] == 'B') & (seq[i+2] == 'T')):
            w = 'LBT'
            cond = 'skip'
        else: 
            w = seq[i]
    else:
        w = seq[i]
    s = s+w
    sout.append(s)
seqout.append(sout)
like image 705
unitedsaga Avatar asked Mar 03 '26 06:03

unitedsaga


1 Answers

You can put ( ) around the first parameter (pattern) in re.split:

import re

seq = re.split(r"(LBT)", "HLHLBTS")
print(seq)

Prints:

['HLH', 'LBT', 'S']
like image 165
Andrej Kesely Avatar answered Mar 05 '26 20:03

Andrej Kesely