Rohith Sharma
Rohith Sharma

Reputation: 43

How to split a string into parts that each part contains only same characters in python

I want to get a sequence of DNA as a string and i need to split the string into parts of a list.And each part must contain same characters only.And final output must be a list according to the order of the original sequence using python 3.4

Ex:- infected ="AATTTGCCAAA" I need to get the output as followed

Modified.  = ['AA','TTT','G','CC','AAA' ]

Upvotes: 1

Views: 87

Answers (2)

Kasravnd
Kasravnd

Reputation: 107287

It's what that itertools.groupby is for :

>>> from itertools import groupby
>>> infected ="AATTTGCCAAA"
>>> 
>>> [''.join(g) for _,g in groupby(infected)]
['AA', 'TTT', 'G', 'CC', 'AAA']

Upvotes: 4

Thilina Chathuranga
Thilina Chathuranga

Reputation: 113

def fchar(ch,mi):
    global numLi
    fc=ch
    li=""
    for c in infected[mi:]:
        if fc==c :
            li+=fc
            mi = mi+1
        else:
            break

    if mi<len(infected) :
        return li+" "+fchar(infected[mi],mi)
    else:
        return li

infected =input("Enter DNA sequence\n") ;#"AAATTTTTTTTGCCCCCCA"
x=fchar(infected[0],0)         
newSet = x.split(' ')
print(newSet)

Upvotes: 1

Related Questions