Samuel Grande
Samuel Grande

Reputation: 21

Replace list using dictionary

Biological RNA:

rna = AGCACGUAGCUGACUGACUGA

protein_chart = {"UUU":"F", "UUC":"F", "UUA":"L", "UUG":"L",
    "UCU":"S", "UCC":"s", "UCA":"S", "UCG":"S",
    "UAU":"Y", "UAC":"Y", "UAA":"STOP", "UAG":"STOP",
    "UGU":"C", "UGC":"C", "UGA":"STOP", "UGG":"W",
    "CUU":"L", "CUC":"L", "CUA":"L", "CUG":"L",
    "CCU":"P", "CCC":"P", "CCA":"P", "CCG":"P",
    "CAU":"H", "CAC":"H", "CAA":"Q", "CAG":"Q",
    "CGU":"R", "CGC":"R", "CGA":"R", "CGG":"R",
    "AUU":"I", "AUC":"I", "AUA":"I", "AUG":"M",
    "ACU":"T", "ACC":"T", "ACA":"T", "ACG":"T",
    "AAU":"N", "AAC":"N", "AAA":"K", "AAG":"K",
    "AGU":"S", "AGC":"S", "AGA":"R", "AGG":"R",
    "GUU":"V", "GUC":"V", "GUA":"V", "GUG":"V",
    "GCU":"A", "GCC":"A", "GCA":"A", "GCG":"A",
    "GAU":"D", "GAC":"D", "GAA":"E", "GAG":"E",
    "GGU":"G", "GGC":"G", "GGA":"G", "GGG":"G",}

I want to split the rna into groups of 3 and replace all of those items with their representative components from the "protein_chart" dictionary, but I can't seem to get it to work using other examples I found.

Help?

Upvotes: 0

Views: 60

Answers (2)

JCollerton
JCollerton

Reputation: 3307

You could use:

# Split the rna vector into chunks of three
rna = 'AGCACGUAGCUGACUGACUGA'
n = 3
rna_split = [rna[i:i+n] for i in range(0, len(rna), n)]

# Go through dictionary looking for entries and printing them
for dna_el in rna_split:
    print(protein_chart[dna_el])

Upvotes: 1

Eugene Soldatov
Eugene Soldatov

Reputation: 10135

You can do it using join and slicing:

print(''.join(protein_chart[x] for x in [rna[y:y+3] for y in range(0, len(rna), 3)]))

Upvotes: 1

Related Questions