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

前端 未结 11 2328
悲&欢浪女
悲&欢浪女 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:45

    with open('demo.txt') as f:
        lines = f.readlines()
    
    l_lines = len(lines)
    
    n_lst = []
    
    for i, line in enumerate(lines):
        line = line.strip()
        if i == l_lines - 1:
            if lines[-2] not in line:
                n_lst.append(line)
            break
        if line not in lines[i + 1]:
            n_lst.append(line)
    
    print(n_lst)
    

    Output

    ['ABCDEFGHIJKLMNO', 'CEST', 'DBTSFDEO', 'EOEUDNBNUW', 'EAEUDNBNUW', 'FGH']
    

提交回复
热议问题