Search code examples
pythonstringlisttuples

Remove part of a string with coordinates in python


Hello I have a list of tuple such as :

indexes_to_delete=((6,9),(20,22),(2,4))

and a sequence that I can open using Biopython :

Sequence1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

and from indexes_to_delete file I would like to remove the part from :

6 to 9
20 to 22

and

2 to 4

so if I follow these coordinate I should have a new_sequence :

A B C D E F G H I J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 

so if I remove the coordinates I get :

A E J  K  L  M  N  O  P  Q  R  S  W  X  Y  Z
1 5 10 11 12 13 14 15 16 17 18 19 23 24 25 26 

Solution

  • indexes_to_delete=((6,9),(20,22),(2,4))
    Sequence1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    
    s = ''.join(ch for i, ch in enumerate(Sequence1, 1) if not any(a <= i <= b for a, b in indexes_to_delete))
    
    print(s)
    

    Prints:

    AEJKLMNOPQRSWXYZ