Python Forum
Replacing characters in a string with a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replacing characters in a string with a list
#1
I am trying to take a string of DNA bases and partition the string into codons followed by replacing each codon one by one with a list of codons while keeping the rest of the sequence untouched.

For instance,

If a DNA sequence is ATG GCC

and the possible codons for replacement are TCC GGG

the possible combinations would be TCC GCC, GGG GCC, ATG TCC and ATG GGG.

So far i've written some code to Splice the DNA sequence into codons

from itertools import zip_longest

def grouper(iterable, n, fillvalue='x'):

    args = [iter(iterable)] * n


    ans = list(zip_longest(fillvalue=fillvalue, *args))

    t = len(ans)
    for i in range(t):
        ans[i] = "".join(ans[i])
    return " ".join(ans)

s = "ccggcgaacccgggcaccaccgccacgtacctc"
k = 3

Splice = grouper(s, k)
print(Splice)
I'm not sure if I can now use Itertools to try to replace each codon with my list or if I need to take an alternative approach?
Reply


Messages In This Thread
Replacing characters in a string with a list - by cjms981 - Dec-30-2019, 05:46 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,520 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  Replacing String Variable with a new String Name kevv11 2 787 Jul-29-2023, 12:03 PM
Last Post: snippsat
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,530 Apr-12-2023, 10:39 AM
Last Post: jefsummers
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,219 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Extract continuous numeric characters from a string in Python Robotguy 2 2,642 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Replacing a words' letters in a string cananb 2 3,466 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,812 Oct-19-2020, 04:16 AM
Last Post: deanhystad
  How to find the first and last of one of several characters in a list of strings? tadsss 2 2,196 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  How to get first two characters in a string scratchmyhead 2 2,092 May-19-2020, 11:00 AM
Last Post: scratchmyhead
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,725 May-15-2020, 01:37 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020