Seperate duplicate in string into list

I have a following string

"TAUXXTAUXXTAUXX"

i want to make a list contains the following

lst = ["TAUXX", "TAUXX", "TAUXX"]

How i make it and is there is a string library in python to do it ?

Thanks in advance.

P.S : I want it in python

Upvotes: 0

Views: 82

Answers (2)

Kelly Bundy
Kelly Bundy

Reputation: 27609

Find the string in its double:

s = 'TAUXXTAUXXTAUXX'

i = (s * 2).find(s, 1)
lst = len(s) // i * [s[:i]]

print(lst)

Output (Try it online!):

['TAUXX', 'TAUXX', 'TAUXX']

Upvotes: 2

Jimmy Wang
Jimmy Wang

Reputation: 67

There are many ways to deal with,

I recommend to use the built-in package: re

import re

test_str = "TAUXXTAUXXTAUXX"

def splitstring(string):
    match= re.match(r'(.*?)(?:\1)*$', string)
    word= match.group(1)
    return [word] * (len(string)//len(word))

splitstring(test_str)

output:

['TAUXX', 'TAUXX', 'TAUXX']

Upvotes: 2

Related Questions