当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Matching codons using lists, dictionaries and loops

问题描述:

This is one of the more difficult problems I have faced so far so excuse my for not providing a substantial attempt at doing it.

I want a program that prints matching codons in the format:

AAA : TTT

GGG : CCC

TTT : AAA

CCC : GGG

.

.

.

Here is what I did:

pairs = {'A':'T','C':'G','T':'A','G':'C'}

codonsA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']

codonsB = ['TAA', 'CTA', 'AAC', 'TTC', 'AGA', 'CAC', 'CCG', 'ATG', 'GCC', 'GTA']

for A in codonsA:

print A + ' :',

for B in codonsB:

print B,

print

#OUTPUT:

AAG : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

TAC : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

CGG : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

GAT : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

TTG : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

GTG : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

CAT : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

GGC : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

ATT : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

TCT : TAA CTA AAC TTC AGA CAC CCG ATG GCC GTA

What I need to do now is to get rid off 9 codons from B from each row on the right of the colon and leave only the one codon on the right that matches the codon from A on the left. How do I do that?

网友答案:

You don't even need that codonsB list. A codon maps onto its anticodon complement 1:1.

def anticodon(codon):
    """returns the anticodon complement for a given codon"""
    return ''.join(pairs[c] for c in codon)

anticodon('AAG')
Out[5]: 'TTC'

You're free to check if each element in codonsA has an anticodon in codonsB, if you need to make that check.

all(anticodon(c) in codonsB for c in codonsA)
Out[6]: True

And the output I think you were originally looking for:

for codon in codonsA:
    print '{} : {}'.format(codon,anticodon(codon))

AAG : TTC
TAC : ATG
CGG : GCC
GAT : CTA
TTG : AAC
GTG : CAC
CAT : GTA
GGC : CCG
ATT : TAA
TCT : AGA
分享给朋友:
您可能感兴趣的文章:
随机阅读: