Remove item from list based on the next item in same list

前端 未结 11 2373
悲&欢浪女
悲&欢浪女 2021-02-18 17:08

I just started learning python and here I have a sorted list of protein sequences (total 59,000 sequences) and some of them overlap. I have made a toy list here for example:

11条回答
  •  遇见更好的自我
    2021-02-18 17:54

    There is other working answers, but none of them explain your actual problem. you were actually really close of a valid solution and what is, in my opinion, the most readable answer.

    The error came from the fact that you were mutating the same list while checking for index using range().

    Thus, while increasing the i variable you were removing item from the list which at one point causes the index error inevitably.

    Therefore, here is a working version of your initial code with some changes,

    pattern = ["ABCDE","ABCDEFG","ABCDEFGH","ABCDEFGHIJKLMNO","CEST","DBTSFDE","DBTSFDEO","EOEUDNBNUW","EAEUDNBNUW","FG","FGH"]
    output_pattern = []
    
    
    for i in range(0, (len(pattern)-1)):
        if not pattern[i] in pattern[i+1]:
            output_pattern.append(pattern[i]) 
    
    # Adding the last item
    output_pattern.append(pattern[-1])   
    print (output_pattern)
    
    >>>> ['ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FGH']    
    

    Note that this code will work if your list is previously sorted as you mentioned in comment section.

    What is this code doing ?

    Basically, it use the same logic of your initial answer where it iterates on the list and check if the next item contains the current item. But, using another list and iterating until the before last item, will fix your index problem. But now comes a question,

    What should I do with the last item ?

    Since the list is sorted, you can consider the last item as always being unique. This is why I'm using

    output_pattern.append(pattern[-1])
    

    which adds the last item of the initial list.

    Important note

    This answer was written in response to OP's initial question where he wanted to keep the longer overlap and I quote based on the next item in same list. As stated by @Chris_Rands if your concerns are related to a biological task and need to find any overlap, this solution is not suited for your needs.

    Example where this code would fail to recognize a potential overlap,

    pattern = ["ACD", "AD", "BACD"]
    

    where it would output the same result without removing the possible "ACD" overlap. Now, just as a clarification though, this would imply a much more complex algorithm and I initially thought it was out of the scope of the question's requirements. If ever this is your case, I may be completely wrong here, but I truly think a C++ implementation seems more appropriate. have a look at the CD-Hit algorithm suggested by @Chris_Rands in the comment section.

提交回复
热议问题