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
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)
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?
I tried simplifying your function for fun:
def grouper(iterable, n, fillvalue='x'):
    codon_lists = zip_longest(*[iter(iterable)]*n, fillvalue=fillvalue)
    return " ".join("".join(codon) for codon in codon_lists)
In general, I'd recommend list and generator comprehensions over reassigning / overwriting your list, as you were doing. In any case it didn't seem relevant to your question...

While it's good your provided code, you should really be excluding code not relevant to the question, and including your attempt at what you're asking for help with. That said, how do you know which codons to replace? The way your question is currently asked, I could answer in a number of ways. If you could give us more detail, we can surely come up with a satisfactory answer.
Feel like you're not getting the answers you want? Checkout the help/rules for things like what to include/not include in a post, how to use code tags, how to ask smart questions, and more.

Pro-tip - there's an inverse correlation between the number of lines of code posted and my enthusiasm for helping with a question :)

Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 213 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Extract continuous numeric characters from a string in Python Robotguy 2 765 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Replacing a words' letters in a string cananb 2 822 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 1,038 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 888 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  How to get first two characters in a string scratchmyhead 2 837 May-19-2020, 11:00 AM
Last Post: scratchmyhead
  Remove escape characters / Unicode characters from string DreamingInsanity 5 4,294 May-15-2020, 01:37 PM
Last Post: snippsat
  Strange Characters in JSON returned string fioranosnake 4 2,297 Dec-02-2019, 07:25 PM
Last Post: fioranosnake
  Split a long string into other strings with no delimiters/characters krewlaz 4 1,156 Nov-15-2019, 02:48 PM
Last Post: ichabod801
  How to iterate over some characters in a string and the others will stay as it is. ? sodmzs 9 2,011 Jun-17-2019, 06:45 PM
Last Post: perfringo

Forum Jump:

User Panel Messages

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