'How to extract a part of a protein sequence in reverse using Python [duplicate]
Suppose I have a list of protein sequences:
VLQSDLYTLSSSVTV
WKLYSKVKPLLNVAR
YNTSLRTMPTMIWTW
GVDFYSTITRARFEE
I want to extract only the last 4 residues (last four characters) from the sequences (in reverse order), such as
VTVS
RAVN
WTWI
EEFR
How to do that with Python code?
Solution 1:[1]
Via a list comprehension:
a = ['VLQSDLYTLSSSVTV', 'WKLYSKVKPLLNVAR', 'YNTSLRTMPTMIWTW', 'GVDFYSTITRARFEE']
b = [x[::-1][0:4] for x in a]
Each x is a string from the list a. The portion x[::-1] reverses that string. The first four characters of the reversed string are extracted via ...[0:4]. The list b contains each of these results.
Solution 2:[2]
s = "VLQSDLYTLSSSVTV WKLYSKVKPLLNVAR YNTSLRTMPTMIWTW GVDFYSTITRARFEE"
words = s.split()
for word in words:
print(word[-1:-5:-1])
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 |
