【问题标题】:Iterativelly combine strings and remove the first strings [duplicate]迭代地组合字符串并删除第一个字符串[重复]
【发布时间】:2021-05-25 22:10:55
【问题描述】:

对于一个给定的字符串,我想将两个两个组合成一组,但是当创建第一个组时,我想删除第一个字母,然后将它们分组。 像这样: 初始字符串:'ACGT' 目标:['AC','CG','GT']

所以我做了这段代码:

for i in range(0, len(seq_example), n):
    out = [seq_example[i:i + n(len(seq_example) - 1)]]
    

但是,缺少一个组合。谁能帮帮我!

【问题讨论】:

    标签: python


    【解决方案1】:

    我在 Python 中找到了解决您问题的方法。

    我使用了您的代码并更改了一些东西,我认为现在它应该可以正常工作了。

    seq_example ='ACGT'
    
    out =[]
    for i in range(len(seq_example)-1):
        out.append(seq_example[i:i+2])
    
    print(out)

    【讨论】:

      【解决方案2】:

      试试:

      seq_example = list('ACGT')
      li = list(map("".join, zip(seq_example, seq_example[1:])))
      

      李:

      ['AC', 'CG', 'GT']
      

      【讨论】:

        【解决方案3】:

        简单的索引就可以完成这项工作

        s = 'ACGT'
        [s[i:i+2] for i in range(len(s)-1)]
        
        ['AC', 'CG', 'GT']
        

        【讨论】:

          【解决方案4】:

          我相信你可以做到:

          seq_example = "ACGT"
          window = 2
          out = [seq_example[i:i+window] for i in range(len(seq_example)-window + 1)]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2012-11-27
            • 1970-01-01
            • 2014-07-06
            • 2011-06-24
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多