Home > Software design >  Seperate duplicate in string into list
Seperate duplicate in string into list

Time:02-01

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

CodePudding user response:

There are sure myriads of various approaches for such splitting. Below one of them:

# I have a following string
s = "TAUXXTAUXXTAUXX"

# I want to make a list contains the following
lst = ["TAUXX", "TAUXX", "TAUXX"]

from sympy.ntheory import primefactors
s_len = len(s)
print(primefactors(s_len))
I, J = primefactors(s_len)[0:2]
lstD = []
for j in range(0, s_len, J):
    lstD.append(s[j:j J])
if all(lstD[0]==item for item in lstD[1:]):
    print(lstD) 

prints

[3, 5]
['TAUXX', 'TAUXX', 'TAUXX']

The idea is to split the string into equal string chunks testing if all of them are the same. If they are then the list of these chunks is the required result.

CodePudding user response:

There are many ways to deal with,

I recommend to use the built-in package: re

import re

# initializing string
test_str = "TAUXXTAUXXTAUXX"
 
# printing original string
print("The original string is : "   test_str)
 
# initializing target
K = 'TAUXX'
 
# Split by repeating substring
# Using re.findall()
res = re.findall(K, test_str)
 
# printing result
print("The split string is : "   str(res))

output:

The original string is : TAUXXTAUXXTAUXX
The split string is : ['TAUXX', 'TAUXX', 'TAUXX']

reference: Python | Split by repeating substring

  • Related