[英]Dictionaries and for loops in functions
我目前正在开发我的第一个网站,这是一个 dna 翻译器,您可以将您的 dna 翻译成某种蛋白质。 为此,我在视图中创建了一个 class,如下所示:
class TranslatorView(View):
template_name = 'main/translated.html'
mapper_1={ #Here's the protein dictionary
"aat": "Asparagine",
"aac": "Asparagine",
"aaa": "Lysine",
"aag": "Lysine",
"act": "Threonine",
"acc": "Threonine",
"aca": "Threonine",
"acg": "Threonine",
"agt": "Serine",
"agc": "Serine",
"aga": "Arginine",
"agg": "Arginine",
"att": "Isoleucine",
"atc": "Isoleucine",
"ata": "Isoleucine",
"atg": "Methionine",
"cat": "Histidine",
"cac": "Histidine",
"caa": "Glutamine",
"cag": "Glutamine",
"cct": "Proline",
"ccc": "Proline",
"cca": "Proline",
"ccg": "Proline",
"cgt": "Arginine",
"cgc": "Arginine",
"cga": "Arginine",
"cgg": "Arginine",
"ctt": "Leucine",
"ctc": "Leucine",
"cta": "Leucine",
"ctg": "Leucine",
"gat": "Aspartic",
"gac": "Aspartic",
"gaa": "Glutamic",
"gag": "Glutamic",
"gct": "Alanine",
"gcc": "Alanine",
"gca": "Alanine",
"gcg": "Alanine",
"ggt": "Glycine",
"ggc": "Glycine",
"gga": "Glycine",
"ggg": "Glycine",
"gtt": "Valine",
"gtc": "Valine",
"gta": "Valine",
"gtg": "Valine",
"tat": "Tyrosine",
"tac": "Tyrosine",
"taa": "Stop",
"tag": "Stop",
"tct": "Serine",
"tcc": "Serine",
"tca": "Serine",
"tcg": "Serine",
"tgt": "Cysteine",
"tgc": "Cysteine",
"tga": "Stop",
"tgg": "Tryptophan",
"ttt": "Phenylalanine",
"ttc": "Phenylalanine",
"tta": "Leucine",
"ttg": "Leucine",
}
def translate_protein(self,phrase):
protein = ""
for letter in phrase:
if letter.lower() in self.mapper_1:
protein += self.mapper_1[letter.lower()].upper() if letter.isupper() else self.mapper_1[letter]
return protein
def get(self, request, *args, **kwargs):
return render(request, 'main/translator.html')
def post(self, request, *args, **kwargs):
protein = request.POST.get('text','protein')
return render(request, self.template_name, {'protein': self.translate_protein(protein)})
我可能在字典和 for 循环中有问题,因为要翻译蛋白质,forloop 需要获得三个字母,因为 codom(rna 字符串)被分组为三个字母。
例子:
AAT 应该翻译成天冬酰胺。 所以 forloop 应该得到 rna 链中的三个字母并将其翻译成 Asparagine。
不用说,Idk 如何做到这一点。 所以需要一些帮助。
顺便说一句,我希望我的解释清楚。
您可以尝试使用以下方法作为您的翻译 function 在您的视图中。 不需要 for 循环,您可以查看Python 切片参考。
def translate_protein(self, phrase):
if not isinstance(phrase, str):
raise ValueError('Phrase should be a string.')
if len(phrase) < 3:
raise ValueError('Phrase should be at least three chars.')
index_str = phrase[:3].lower()
# The following will return None if no protein is found based on the index str.
return self.mapper_1.get(index_str, None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.