Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 字典和for函数中的循环_Python_Django - Fatal编程技术网

Python 字典和for函数中的循环

Python 字典和for函数中的循环,python,django,Python,Django,我目前正在开发我的第一个网站,这是一个dna翻译,你可以将你的dna翻译成某种蛋白质。为此,我在视图中创建了一个类,如下所示: class TranslatorView(View): template_name = 'main/translated.html' mapper_1={ #Here's the protein dictionary "aat": "Asparagine&

我目前正在开发我的第一个网站,这是一个dna翻译,你可以将你的dna翻译成某种蛋白质。为此,我在视图中创建了一个类,如下所示:

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-loop中遇到了问题,因为要翻译蛋白质,forloop需要三个字母,因为codom(rna字符串)分为三个字母

例如:

AAT应翻译成天冬酰胺。所以forloop应该有rna链中的三个字母,并将其翻译成天冬酰胺

不用说,我知道怎么做。所以需要一些帮助


顺便说一句,我希望解释清楚。

您可以尝试使用以下方法作为您视图中的翻译功能。不需要for循环,您可以检查

def-translate_蛋白质(自身,短语):
如果不存在(短语,str):
raise VALUERROR('短语应为字符串')
如果len(短语)<3:
raise VALUERROR('短语应至少为三个字符')
索引=短语[:3]。下()
#如果根据索引str未找到任何蛋白质,则以下内容将返回None。
返回self.mapper_1.get(index_str,None)

您的
请求.POST
看起来怎么样?你能添加有效载荷吗?它在代码BTW中,
self.mapper_1[letter.lower()]如果letter.isupper()否则self.mapper_1[letter]
只能是
self.mapper_1[letter.lower()]
I don;在你的问题@MarcJuegos_YtI中看不到任何样本帖子有效负载我没有任何有效负载当我输入一个应该找到的蛋白质时,它不会返回任何。我知道,如果没有发现蛋白质,它就不会返回任何结果。但在这种情况下,它应该被找到,然后它会像预期的那样起作用,因为第二个参数只是为了防止用户给出错误的蛋白质时找不到它。下面是python dict get()方法的详细信息。但事情是这样的。我输入了一个好的输入字符串,你使用的是哪个输入字符串(相位值)?当我在编写网页时,输入字符串是用html编码的文本区域。这就是你要问的吗。我是个业余编码爱好者