使用字典转换字母 [英] Converting letters using a dictionary
问题描述
我想写一个程序,将字典中匹配字符的字符转换为与该字符相关联的值。如果字典是{'A':'T','C':'G','T':'A','G':'C'},字符串是'AAG',输出应该是'TTC '。
编辑:这是我现在感谢你的一些答案:
def matching_codons(complementments,poolA):
answer = []
codon =''
counter = 0
for i in poolA:
for a in i:
codon + = complements [a]
counter + = 1
if counter == 3:
answer.append(codon)
不幸的是,这只能翻译前3个字母 - 如何让它继续运行循环? p>
注意:poolA是字符串列表,例如['AAG','TAC','CGG','GAT','TTG','GTG','CAT','GGC','ATT','TCT']
注意2:我不能硬编码任何像翻译表,因为技术上可以更改字典输入
另一种解决方案是使用字符串中的maketrans
from string import maketrans
complementTrans = maketrans(ACTG,TGAC)
poolA = ['AAG ','TAC','CGG','GAT','TTG','GTG','CAT','GGC','ATT','TCT']
[codon.translate密码子in poolA]
您将获得:
['TTC','ATG','GCC','CTA','AAC','CAC','GTA','CCG','TAA','AGA']
奖金
biopython库,例如
poolA = ['AAG','TAC','CGG','GAT','TTG','GTG','CAT','GGC','ATT','TCT']
来自Bio.Seq import Seq
来自Bio.Alphabet import IUPAC
[str(Seq(seq,IUPAC.unambiguous_dna).complement())for seq in poolA]
您会得到相同的结果
Bonus 2
修复您的代码,删除不必要的变量 counter
poolA = ['AAG','TAC','CGG','GAT','TTG','GTG','CAT' GGC','ATT','TCT']
complements = {'A':'T','C':'G','T':'A','G':'C'}
def match_codons(complementments,poolA):
answer = []
for i in poolA:
codon =''迭代
for a in i:
codon + = complements [a]
answer.append(codon)#在次要外部,密码子有三个结束迭代
return answer
matching_codons(complementments,poolA)
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
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)
这篇关于使用字典转换字母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!