Let's go the beastboy Let's go the beastboy - 1 year ago 91
Python Question

DNA restrictions python

def restrict(strand,dna,match):

name=[]
for i in range(len(match)):
indice=strand.find(match[i])
name+=[(dna[i],indice)]
return name
print(restrict_('TCGAGAATTCTCGA',['EcoRI','TaqI'],['GAATTC','TCGA']))


For the example I provided, it should return
[('EcoRI',[4]),('TaqI',[])]
however it returns
[('EcoRI', 4), ('TaqI', 0)]

Answer Source

Updated in response to question edits

You can just count the number of matches using str.count to get the number of matches, and then only output those with exactly one match:

enzyme=[]
for i in range(len(res)):
    num_occur = DNA.count(seq[i])
    index = DNA.find(seq[i]) if num_occur == 1 else []
    enzyme+=[(res[i],index)]
return enzyme

This would return the following on your example query:

>>> one_cutters('TCGAGAATTCTCGA',['EcoRI','TaqI'],['GAATTC','TCGA'])
[('EcoRI', [4]), ('TaqI', [])]