【问题标题】:Converting nucleotides to the corresponding DNA sequence将核苷酸转化为相应的 DNA 序列
【发布时间】:2018-04-12 15:47:49
【问题描述】:

我正在用 python 做一个作业。我必须找到一个函数或程序将这个列表转换为相应的 DNA 序列。

bases_list = ['Adenosine', 'Guanine', 'Thymine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine']

print(dna)

AGTCTAGCTAG  #the output

我想也许我可以创建一个定义函数来将这些碱基转换为相应的字母。我怎样才能做到这一点?有任何想法吗?

PS:最近才开始学python,希望大家帮帮我:)

【问题讨论】:

  • print("".join([x[0] for x in bases_list]))
  • 但是我必须打印为 print(dna),这意味着我必须找到一个我应该调用为“dna”的函数。你认为我必须写一个 def 函数吗?
  • 我认为你需要先学习 Python 编程的基础知识。
  • "...一个find函数的赋值..." ?
  • 如果您的问题得到解答,请accept the most helpful answer。谢谢。

标签: python list function bioinformatics


【解决方案1】:

最简洁:

print("".join([x[0] for x in bases_list]))

dna = "".join(x[0] for x in bases_list])
print(dna)

否则:

dna = ""
for base in bases_list:
    dna += base[0] . # get 1st letter from base
print(dna)

【讨论】:

    【解决方案2】:

    让我们使用字符串索引和join:

    dna = ''.join(i[0] for i in bases_list)
    print(dna)
    

    输出:

    AGTCTAGCTAG
    

    【讨论】:

    • 连接语法实际上是什么?我在我们的课程中从未见过这样的:)
    • @berr Join 从一个可迭代对象中获取每个元素,并将它们连接成一个带有或带有分隔符的字符串。 See docs
    【解决方案3】:

    map 怎么样?

    In [276]: ''.join(map(lambda x: x[0], bases_list))
    Out[276]: 'AGTCTAGCTAG'
    

    str.join 用于获取字符串列表(或任何可迭代的)并将它们连接在一起。 map 将相同的函数应用于迭代中的每个元素。

    快速笔记。我在终端上运行了这段代码,但如果你在脚本中运行它,不要忘记将结果分配回另一个变量。

    seq = ''.join(map(lambda x: x[0], bases_list))
    print(seq)
    

    【讨论】:

    • 不错,适合高级用户 ;) 我正在考虑另一种简单的方法,但我没有使用 Ideas,您还有其他方法吗?
    • @DamianLattenero 不,我相信这些答案中已经讨论了每个简单的选项。其他任何事情都需要一些思考:)
    【解决方案4】:

    是的,有几个!当你不久前开始的时候,让我们做一些简单的事情:

    bases_list = ['Adenosine', 'Guanine', 'Thymine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine']
    
    dna = "" # You begin with the dna empty
    
    for sequence in bases_list: # you go throw every chain
      dna += sequence[0] # you add the first letter with the op [0] of lists
    
    print(dna) # finally print
    

    你可以在这里阅读循环的基础https://wiki.python.org/moin/ForLoop

    【讨论】:

      【解决方案5】:

      为了完整起见,我想我会添加 itemgetter https://docs.python.org/3/library/operator.html#operator.itemgetter 解决方案。

      在这种情况下,它获取bases_list 中每个元素的索引(0) 中的字符。据说比lambdaoperator.itemgetter or lambda更快更干净

      from operator import itemgetter
      
      bases_list = ['Adenosine', 'Guanine', 'Thymine', 'Cytosine', 'Thymine',
                    'Adenosine', 'Guanine', 'Cytosine', 'Thymine', 'Adenosine', 'Guanine']
      dna = "".join((map(itemgetter(0),bases_list)))
      
      print (dna)
      

      结果:

      AGTCTAGCTAG
      

      【讨论】:

        【解决方案6】:

        如果您想要一个将基名称转换为其对应字母的函数,那么您在这里很幸运,因为它只是基名称的第一个字母。

        通过“索引”轻松获取字符串的第一个字母。例如:

        base = "Adenosine"
        letter = base[0]
        # letter should now be "A"
        

        在这样一个简单的情况下,人们可能不会费心定义一个特定的函数,但它是可能的,看起来如下:

        def base2letter(base):
            """This function converts the name of a base into the corresponding letter."""
            return base[0]
        

        现在您可以将该函数应用于列表中的每个基本名称。在 python 中执行此操作的常用方法是使用“列表推导”:

        letters_list = [base2letter(base) for base in bases_list]
        

        您现在拥有的是字母列表:["A", "G", "T", "C", "T", "A", "G", "C", "T", "A", "G"]

        下一步是将它们连接成一个字符串。一种有效的方法是使用为字符串定义的join 方法。它将字符串粘贴在给定的连续元素之间:"_".join(["A", "B", "C"]) 将导致"A_B_C"。在这里你想把字母放在一起,它们之间没有任何东西,所以你必须使用空字符串:

        dna = "".join(letters_list)
        

        请注意,可以通过将新字母一一连接到字符串的当前状态,在 for 循环中逐步构建字符串(请参阅https://stackoverflow.com/a/47036161/1878788)。如果 Python 只是用来让您大致了解使用“命令式”风格的编程语言如何工作,那么您可能会期望这样做。但是,它在 python 中效率不高,这就是为什么许多答案没有使用这种技术。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          • 2017-09-29
          • 2020-09-15
          • 2023-02-19
          • 2021-07-06
          • 1970-01-01
          相关资源
          最近更新 更多