问题
I'm trying to write a program that converts letters matching a key in a dictionary to the value associated with that key e.g. if the dictionary is {'A':'T', 'C':'G', 'T':'A', 'G':'C'} and the string is 'AAG' the output should be 'TTC'.
EDIT: This is what I've got now thanks to some of your answers:
def matching_codons(complements, poolA):
answer = []
codon = ''
counter = 0
for i in poolA:
for a in i:
codon+= complements[a]
counter += 1
if counter == 3:
answer.append(codon)
Unfortunately this only translates the first 3 letters - how can I make it keep running through the loop?
Note: poolA is a list of strings e.g. ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
Note2: I can't hard-code anything like a translation table because technically the dictionary input can be changed
回答1:
another solution is using maketrans from string
from string import maketrans
complementTrans = maketrans("ACTG", "TGAC")
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
[codon.translate(complementTrans) for codon in poolA]
you get:
['TTC', 'ATG', 'GCC', 'CTA', 'AAC', 'CAC', 'GTA', 'CCG', 'TAA', 'AGA']
Bonus
It is always better to use biopython library, for example
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
from Bio.Seq import Seq
from Bio.Alphabet import IUPAC
[str(Seq(seq, IUPAC.unambiguous_dna).complement()) for seq in poolA]
you get the same result
Bonus 2
Fixing your code, I remove unnecessary variable counter
poolA = ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']
complements = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
def matching_codons(complements, poolA):
answer = []
for i in poolA:
codon = '' # inside of for, codon reset in each iteration
for a in i:
codon+= complements[a]
answer.append(codon) # outside of secondary for, codon have three leters to end iterations
return answer
matching_codons(complements, poolA)
回答2:
Define your dictionary:
>>> d = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
Now, using that dictionary, AAG
can be translated as follows:
>>> ''.join(d[c] for c in 'AAG')
'TTC'
回答3:
So, to take your example here:
d = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
x = "AAG"
To get your result to output "TTC", you can do this:
s = ""
for i in x:
s += d.get(i)
A one liner for this:
y = "".join([d.get(i) for i in x])
回答4:
Assumptions: complements is the dictionary {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
You've got a pretty good gameplan so far, you're only missing a few ideas:
1) complements.values() will give you a view of all the values in the dict (see this). You want to instead look up the particular value for the character you are on. As you have already checked if the key is in the dictionary with "if a in complements:", you can look it up with "compliments[a]".
2) Once you've built a complemented codon, you can add it to your list of answers and clear the codon you are building at the moment.
You should note that there are cleaner answers posted here, but I wanted to keep as much of your code as possible. Please read through the other answers here and try to understand the better methods of achieving your goal as well.
If you have any more questions, please feel free to ask!
回答5:
Consider making a translation table:
>>> from string import maketrans
>>> transtab = maketrans("ACTG", "TGAC")
>>> [s.translate(transtab) for s in ['AAG', 'TAC', 'CGG', 'GAT', 'TTG', 'GTG', 'CAT', 'GGC', 'ATT', 'TCT']]
['TTC', 'ATG', 'GCC', 'CTA', 'AAC', 'CAC', 'GTA', 'CCG', 'TAA', 'AGA']
回答6:
use function:
def StringChange(str):
dic = {'A':'T', 'C':'G', 'T':'A', 'G':'C'}
output = []
for i in str:
output += dic[i]
print("".join(output))
It's done. You can use StringChange("AAG"), then get the result "TTC"
来源:https://stackoverflow.com/questions/32685628/converting-letters-using-a-dictionary