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
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  doing string split with 2 or more split characters Skaperen 22 2,320 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  Replacing String Variable with a new String Name kevv11 2 729 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,430 Apr-12-2023, 10:39 AM
Last Post: jefsummers
Question [SOLVED] Delete specific characters from string lines EnfantNicolas 4 2,143 Oct-21-2021, 11:28 AM
Last Post: EnfantNicolas
  Extract continuous numeric characters from a string in Python Robotguy 2 2,583 Jan-16-2021, 12:44 AM
Last Post: snippsat
  Replacing a words' letters in a string cananb 2 3,410 Dec-01-2020, 06:33 PM
Last Post: perfringo
  Python win32api keybd_event: How do I input a string of characters? JaneTan 3 3,727 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,142 Jun-02-2020, 05:23 PM
Last Post: bowlofred
  How to get first two characters in a string scratchmyhead 2 2,038 May-19-2020, 11:00 AM
Last Post: scratchmyhead
  Remove escape characters / Unicode characters from string DreamingInsanity 5 13,423 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