【问题标题】:How can I find occurences in a list? [duplicate]如何在列表中查找事件? [复制]
【发布时间】:2018-04-27 15:23:32
【问题描述】:

我正在使用 Python 进行分配,如果您能回答,我有一个问题。 我想编写一个函数,它返回一个列表,其中包含序列中所有“ATG”出现的第一个核苷酸的位置。 例如,我们可以说我们的 DNA 序列是 AATGCATGC。我们看到 ATG 可以从索引 1 开始,另一种可能是索引 5。 我试过这个来解决这个任务;

dna = "AATGCATGC"
starting_offset = dna.index("ATG")
print(starting_offset)

我得到的结果是 1。但我想得到结果为 [1, 5]

那么我应该如何为所有事件编写这个函数呢?

谢谢你帮助我:)

【问题讨论】:

  • 听起来像是 itertools 的工作 - 搜索 python 和 itertools 并查看 api
  • 如果你想自己做:找到第一个出现 [x] - 匹配匹配中不可能(找不到从一个查找开始的 ATG)所以从你的查找位置创建一个较短的字符串+ len(ATG) 并找到下一个索引。累积它们直到剩下少于 len(ATG) 个字符。
  • @strawberry:这种方法的例子见我的回答

标签: python list function return bioinformatics


【解决方案1】:

使用正则表达式,您可以使用 re.finditer 查找所有出现:

你可以试试这个功能:

import re
text = 'AATGCATGC'
pattern='ATG'
def getIndexes (text,pattern):
    list=[index.start() for index in re.finditer('ATG', text)]
    return list
getIndexes(text,pattern)
>>[1, 5]

它将为您提供您正在寻找的列表。希望这会有所帮助!

【讨论】:

    【解决方案2】:

    如果你想思考什么,分析一下:

    def GetMultipleInString(dna, term):
        # computing end condition 0
        if (term not in dna):
            print (dna + " does not contain the term " + term)
            return []
    
        # start of list of lists of 2 elements: index, rest
        result = [[None,dna]]
    
        # we look for the index in the rest, need to keep track how much we
        # shortened the string in total so far to get index in complete string
        totalIdx = 0
    
        # we look at the last element of the list until it's length is shorter
        # than the term we look for (end of computing condition 1)
        termLen = len(term)
    
        while len(result[-1][1]) >= termLen:
            # get the last element
            last = result[-1][1]
            try:
                # find our term, if not found -> exception
                idx = last.index(term) 
                # partition "abcdefg" with "c" -> ("ab","c", "defg")
                # we take only the remaining 
                rest = last.partition(term)[2] 
                # we compute the total index, and put it in our result
                result.append( [idx+totalIdx , rest] ) 
                totalIdx += idx+termLen 
            except:
                result.append([None,last])
                break
    
        # any results found that are not none? 
        if (any( x[0] != None for x in result)):
    
            print (dna + " contains the term " + term + " at positions:"),
            # get only indexes from our results
            rv = [ str(x[0]) for x in result if x[0] != None]
            print (' '.join(rv))
    
            return rv
    
        else:
            print (dna + " does not contain the term " + term)
            return []
    
    print("_----------------------------------_")
    myDna = "AATGCATGC"  
    res1 = GetMultipleInString(myDna,"ATG")   
    print(res1)
    
    res2 = GetMultipleInString(myDna,"A")
    print(res2)
    

    【讨论】:

      猜你喜欢
      • 2013-03-29
      • 2021-10-19
      • 2011-08-21
      • 2020-05-11
      • 2021-10-19
      • 2019-12-31
      • 2018-09-26
      • 2010-11-04
      • 2014-01-08
      相关资源
      最近更新 更多