首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何清理模板中的列表

如何清理模板中的列表
EN

Stack Overflow用户
提问于 2020-11-29 02:23:13
回答 3查看 45关注 0票数 2

我目前正在开发我的第一个网站。这是一个rna到蛋白质翻译器。它所做的就是你输入一条dna链,然后它将其翻译成蛋白质。然而,当我得到蛋白质时,它看起来是这样的:

正如你所看到的,蛋白质没有被清理,这意味着它不是以Isoleucine,glycine的形式出现,而是以['Isoleucine','Glycine']的形式出现。这是它的代码(我猜输出部分有问题):

代码语言:javascript
复制
class TranslatorView(View):
    template_name = 'main/translated.html'

    rna_mapper = {
        "a": "u",
        "t": "a",
        "c": "g",
        "g": "c"
    }

    amino_mapper={
        "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(self, phrase):
        translation = ""
        for letter in phrase:
            if letter.lower() in self.rna_mapper:
                translation += self.rna_mapper[letter.lower()].upper() if letter.isupper() else self.rna_mapper[letter]
        return translation

    def translate_amino(self, codon):
        return self.amino_mapper.get(codon, "")
    
    def build_protein(self, phrase): #Here's where I think the problem is
        protein = []
        i = 0
        while i < len(phrase):
            codon = phrase[i: i + 3]
            amino = self.translate_amino(codon)
            if amino:
                protein.append(amino)
            else:
                print(f"The codon {codon} is not in self.mapper_1")
            i += 3
        return protein

    def get(self, request, *args, **kwargs):
        return render(request, 'main/translator.html')

    def post(self, request, *args, **kwargs):
        phrase = request.POST.get('text', 'translation')
        protein = request.POST.get('text','protein')
        return render(request, self.template_name, {'translation': self.translate(phrase), 'protein': self.build_protein(protein)})

如果您需要了解更多的模板,可以参考以下内容:

代码语言:javascript
复制
{% extends "base.html"%}

{% block content%}

<div >
    
    <h2 class = "display-3">DNA TRANSLATED SUCCESFULLY </h2>
    <br>
    <br>
    <br>

  
    <h2>
        {{ translation }}
    </h2>

    <br>
    <br>
    <br>
   
    <h2 class = "display-4">YOUR PROTEIN IS</h2>

    <div class = "protein_image"></div>

    <br>
    <br>

    <h2>
        {{ protein }} 
    </h2>





    <button class= "button_with_image_save" value="Back" onclick="window.history.back()" ></button>
代码语言:javascript
复制
EN

回答 3

Stack Overflow用户

发布于 2020-11-29 02:26:04

我非常确定您正在寻找join函数:

代码语言:javascript
复制
class TranslatorView(View):
    ...
    def post(self, request, *args, **kwargs):
        ...
        return render(request, self.template_name, {'translation': self.translate(phrase), 'protein': ", ".join(self.build_protein(protein))})
票数 1
EN

Stack Overflow用户

发布于 2020-11-29 02:28:10

在模板中只使用join tag

代码语言:javascript
复制
{{ protein|join:", " }}

或者,如果您需要一个html列表,那么您可能应该使用for loap

代码语言:javascript
复制
<ul>
{% for i in protein %}
<li>{{ i }}</li>
{% empty %}
<li>no protein found</li>
{% endfor %}
</ul>
票数 1
EN

Stack Overflow用户

发布于 2020-11-29 02:29:49

如下所示更改build_protein:

代码语言:javascript
复制
    def build_protein(self, phrase): #Here's where I think the problem is
        protein = ''
        i = 0
        while i < len(phrase):
            codon = phrase[i: i + 3]
            amino = self.translate_amino(codon)
            if amino:
                protein = f"{protein},{amino}" 
            else:
                print(f"The codon {codon} is not in self.mapper_1")
            i += 3
        return protein
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65053069

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档