Changing a string iteratively

  • A+

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 :


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) 


>>> for r in rotations('ACGT'): ...     print(r) ...  ACGT TACG GTAC CGTA 


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: