2016-08-31 52 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'] 
相關問題