I am trying to write in Python a script that takes a string and returns a list of string by iteratively moves the letters in the original sequence.
For example : ACGT-->TACG -->GTAC-->CGTA-->STOP (original sequence)
I am a beginner in programming, here is what I've succeed to do
liste=[]
seq=list('ACGT')
nseq=list("test")
while nseq!=seq:
for i in range (0, len(seq)):
nseq[i]=seq[i-1]
''.join(nseq)
liste.append(nseq)
It only returns the first step like TACG and doesn't go on.
Output : ['T', 'A', 'C', 'G']multiple time (endless loop)
Expected output :
TACG
GTAC
CGTA
Basically, you need to move the last element to the beginning until you come to the original sequence. For this collections.deque and it's rotate() method (which is equivalent to d.appendleft(d.pop())) should be the most efficient:
from collections import deque
def rotations(s):
yield s
d = deque(s)
for _ in range(len(d) - 1):
d.rotate(1)
yield ''.join(d)
Demo:
>>> for r in rotations('ACGT'):
... print(r)
...
ACGT
TACG
GTAC
CGTA
Simple solution without any import.
def rot(s, n):
return s[-n:] + s[:-n]
for i in range(4):
print(rot('ACGT', i))
Output
ACGT
TACG
GTAC
CGTA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With