2016-08-31 34 views
3

我写了一个小程序来执行以下操作。我想知道是否有明显更优化的解决方案:将字符串列表拆分为与字符串不同列表匹配的位置

1)取2个字符串列表。通常,第二个列表中的字符串将比第一个列表中的字符串长,但不保证

2)返回从第二个列表派生的字符串列表,该列表已从第一个列表中删除任何匹配的字符串。因此,该列表将包含< =第二个列表中字符串的长度。

下面我显示的内容我谈论的图片例如: basic outline of what should happen

到目前为止,我这就是我。它似乎工作正常,但我只是好奇,如果有一个更优雅的解决方案,我错过了。顺便说一下,我会跟踪字符串每个开始和结束的“位置”,这对于本程序的后续部分很重要。

def split_sequence(sequence = "", split_seq = "", length = 8): 
    if len(sequence) < len(split_seq): 
     return [],[] 
    split_positions = [0] 
    for pos in range(len(sequence)-len(split_seq)): 
     if sequence[pos:pos+len(split_seq)] == split_seq and pos > split_positions[-1]: 
      split_positions += [pos, pos+len(split_seq)] 
    if split_positions[-1] == 0: 
     return [sequence], [(0,len(sequence)-1)] 
    split_positions.append(len(sequence)) 
    assert len(split_positions) % 2 == 0 
    split_sequences = [sequence[split_positions[_]:split_positions[_+1]] for _ in range(0, len(split_positions),2)] 
    split_seq_positions = [(split_positions[_],split_positions[_+1]) for _ in range(0, len(split_positions),2)] 
    return_sequences = [] 
    return_positions = [] 
    for pos,seq in enumerate(split_sequences): 
     if len(seq) >= length: 
      return_sequences.append(split_sequences[pos]) 
      return_positions.append(split_seq_positions[pos]) 
    return return_sequences, return_positions 





def create_sequences_from_targets(sequence_list = [] , positions_list = [],length=8, avoid = []): 
    if avoid: 
     for avoided_seq in avoid: 
      new_sequence_list = [] 
      new_positions_list = [] 
      for pos,sequence in enumerate(sequence_list): 
       start = positions_list[pos][0] 
       seqs, positions = split_sequence(sequence = sequence, split_seq = avoided_seq, length = length) 
       new_sequence_list += seqs 
       new_positions_list += [(positions[_][0]+start,positions[_][1]+start) for _ in range(len(positions))] 
     return new_sequence_list, new_positions_list 

输出示例:

In [60]: create_sequences_from_targets(sequence_list=['MPHSSLHPSIPCPRGHGAQKA', 'AEELRHIHSRYRGSYWRTVRA', 'KGLAPAEISAVCEKGNFNVA'],positions_list=[(0, 20), (66, 86), (136, 155)],avoid=['SRYRGSYW'],length=3) 
Out[60]: 
(['MPHSSLHPSIPCPRGHGAQKA', 'AEELRHIH', 'RTVRA', 'KGLAPAEISAVCEKGNFNVA'], 
[(0, 20), (66, 74), (82, 87), (136, 155)]) 
+0

'string.split()'接受一个子串分隔符。你的算法看起来像你可以迭代分隔字符串。 –

回答

4

让我们来定义这个字符串,s,并且该字符串列表list1删除:

>>> s = 'NowIsTheTimeForAllGoodMenToComeToTheAidOfTheParty' 
>>> list1 = 'The', 'Good' 

现在,让我们去掉那些字符串:

>>> import re 
>>> re.split('|'.join(list1), s) 
['NowIs', 'TimeForAll', 'MenToComeTo', 'AidOf', 'Party'] 

上面的一个强大功能是list1中的字符串可以包含正则表达式活动字符。这也可能是不可取的。正如约翰·拉ROOY在评论中指出,在list1字符串可以处于非活动状态与:

>>> re.split('|'.join(re.escape(x) for x in list1), s) 
['NowIs', 'TimeForAll', 'MenToComeTo', 'AidOf', 'Party'] 
+1

在你加入之前,你应该总是're'escape()'list1'的项目。 –

+1

@JohnLaRooy好点。我喜欢正则表达式的角色,但你绝对正确,不是每个人都会这样做。回答用're.escape'解决方案更新。 – John1024

4

使用正则表达式简化了代码,但它可能是也可能不是更高效。

>>> import re 
>>> sequence_list = ['MPHSSLHPSIPCPRGHGAQKA', 'AEELRHIHSRYRGSYWRTVRA', 'KGLAPAEISAVCEKGNFNVA'],positions_list=[(0, 20), (66, 86), (136, 155)] 
>>> avoid = ['SRYRGSYW'] 
>>> rex = re.compile("|".join(map(re.escape, avoid))) 

得到这样的位置(您需要将您的偏移量添加到这些)

>>> [[j.span() for j in rex.finditer(i)] for i in sequence_list] 
[[], [(8, 16)], []] 

得到新的字符串这样

>>> [rex.split(i) for i in sequence_list] 
[['MPHSSLHPSIPCPRGHGAQKA'], ['AEELRHIH', 'RTVRA'], ['KGLAPAEISAVCEKGNFNVA']] 

或扁平列表

>>> [j for i in sequence_list for j in rex.split(i)] 
['MPHSSLHPSIPCPRGHGAQKA', 'AEELRHIH', 'RTVRA', 'KGLAPAEISAVCEKGNFNVA']