Code purpose:
I provide a string and match it with dictionary keys; if the key and string match I print the dictionary values.
Here's the code:
def to_rna(dna_input):
dna_rna = {'A':'U', 'C':'G', 'G':'C', 'T':'A'}
rna = []
for key in dna_rna.iterkeys():
if key in dna_input:
rna.append(dna_rna[key])
print "".join(rna)
to_rna("ACGTGGTCTTAA") #the string input
Problem:
The result should be 'UGCACCAGAAUU' but all I get is 'UGAC'. The problem appears to be that I have duplicate characters in the string and the loop is ignoring this. How do I loop through the dictionary so that it returns the dictionary value as many times as the dict key is found?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…