StephenMcM190
StephenMcM190

Reputation: 23

For the inner lists break string down into 3 character acronyms

def testing(dnaStrand):
    
protein = []

start = dnaStrand.find('ATG')
    
end = len(dnaStrand)
    
totalLength = dnaStrand[start:end]
    
remove = totalLength.split('TAG')
    
protein = [[i] for i in remove]
    
print(protein)

protein is a list of lists - Each time 'TAG' appears in the given string a new inner list is created and contains all characters up till the next 'TAG'. I want my inner lists to be groups of three characters. For example:

testing('ATGABCTAGDEFHIJTAGKLM')

returns:

[['ATGABC'], ['DEFHIJ'], ['KLM']]

What I need:

[['ATG', 'ABC'], ['DEF', 'HIJ'], ['KLM']]

The input can be any combination of letter, there can be unlimited inner lists and there can be unlimited items inside the inner lists.

Upvotes: 0

Views: 51

Answers (3)

Yash Makan
Yash Makan

Reputation: 764

Try this

def testing(dnaStrand):
    protein=dnaStrand.split('TAG')
    for i in range(len(protein)):
        if len(protein[i]) > 3:
            protein[i]=[protein[i][:3],protein[i][3:]]
        else:
            protein[i]=[protein[i][:3]]
    print(protein)
testing('ATGABCTAGDEFHIJTAGKLM')

Edit:

def testing(dnaStrand):
    protein=dnaStrand.split('TAG')
    for i in range(len(protein)):
        if len(protein[i]) > 3:
            protein[i]=[protein[i][:3],protein[i][3:]]
        else:
            protein[i]=[protein[i][:3]]
    return protein
print(testing('ATGABCTAGDEFHIJTAGKLM'))

OUTPUT

>>> [['ATG', 'ABC'], ['DEF', 'HIJ'], ['KLM']]

Upvotes: 1

ppwater
ppwater

Reputation: 2277

Try this:

def testing(dnaStrand):
    protein = []
    start = dnaStrand.find('ATG')
    end = len(dnaStrand)
    totalLength = dnaStrand[start:end]
    remove = totalLength.split('TAG')
    for i in remove: # every item in remove
        appendlist = [i[:3],i[3:]] # split with 3 chars
        if appendlist[1] == '': # if the item is empty, 
            del appendlist[1] # delete it
        protein.append(appendlist) # then append it to protein
    print(protein)
testing('ATGABCTAGDEFHIJTAGKLM')

Upvotes: 1

Huy Nguyễn
Huy Nguyễn

Reputation: 28

You can use list comprehension, try this instead:

def testing(dnaStrand):
    protein = []
    start = dnaStrand.find('ATG')
    end = len(dnaStrand)
    totalLength = dnaStrand[start:end]

    remove = totalLength.split('TAG')
    for str in remove:
        split_str = [str[i:i+3] for i in range(0, len(str), 3)]
        protein.append(split_str)    
    print(protein)

testing('ATGABCTAGDEFHIJTAGKLM')

Upvotes: 1

Related Questions