How do I replace characters in a string using a character map?

shaz

Code purpose:

I provide a string and match it with dictionary keys; if the key and string match I print the dictionary values.

Here's the code:

def to_rna(dna_input):
    dna_rna = {'A':'U', 'C':'G', 'G':'C', 'T':'A'}
    rna = []
    for key in dna_rna.iterkeys():
        if key in dna_input:
            rna.append(dna_rna[key])
    print "".join(rna)

to_rna("ACGTGGTCTTAA") #the string input

Problem:

The result should be 'UGCACCAGAAUU' but all I get is 'UGAC'. The problem appears to be that I have duplicate characters in the string and the loop is ignoring this. How do I loop through the dictionary so that it returns the dictionary value as many times as the dict key is found?

jgritty

If you want to output a character for every character in dna_input you need to iterate over character in dna_input. Note that the get() function provides a default for characters that aren't in your dictionary. I am replacing with nothing, if desired you could put an n here, or an X.

rna.append(dna_rna.get(char, 'n'))

Your code was only iterating over the 4 entries in the dna_rna dictionary.

def to_rna(dna_input):
    dna_rna = {'A':'U', 'C':'G', 'G':'C', 'T':'A'}
    rna = []
    for char in dna_input:
        rna.append(dna_rna.get(char, ''))
    print "".join(rna)

to_rna("ACGTGGTCTTAA") #the string input

However, this isn't the most efficient way to translate a string.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I replace the last occurrence of a character in a string using sed?

From Dev

How do I replace characters before a specific character?

From Dev

How do I replace a multiple sets of characters with a single character

From Dev

How do I complete the Gnome Character Map with missing Unicode characters?

From Dev

How do I replace a single character in a string in constant time and using no additional space?

From Dev

How do I replace a single character in a string in constant time and using no additional space?

From Dev

How do I replace two or more instances of a character in an Excel string?

From Dev

How do I replace characters in every string in my list in java?

From Dev

How do I replace space characters in a string with "%20"?

From Dev

How do i search & replace using sed and not include a group of characters?

From Dev

How do I replace non word characters with a dash using TSQL?

From Dev

How do I replace a character in a string that repeats many times with a different character in python?

From Dev

How can I replace some characters with `'` character in a text file using sed?

From Dev

C#: Replace Certain Characters if they are the First Character of the String Using Regex

From Dev

How to replace character in the string using regex in java?

From Dev

How do I replace the " character in Java?

From Dev

How can I replace certain characters in a string?

From Java

How to replace a character in C# string ignoring other characters?

From Dev

How do I replace illegal characters in a filename?

From Dev

How do I replace characters with a variable in CMD?

From Dev

How do I replace a comma to "\," in a string using sed

From Dev

How do I replace HTML content with a string in python using BeautifulSoup?

From Dev

How do I replace a character which is not preceded by \ by [character] globally?

From Dev

How do i Replace every n-th character x from an String

From Dev

How do I "unzoom" the currently selected character in Windows Character Map?

From Dev

How do I iterate through a map that has value, Map<String,Integer> declared as Map<String,Map<String,Integer>>using Expression Language ?

From Dev

How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

From Dev

How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

From Dev

How can I match characters in a string and palce a dot after each Letter using preg replace

Related Related

  1. 1

    How do I replace the last occurrence of a character in a string using sed?

  2. 2

    How do I replace characters before a specific character?

  3. 3

    How do I replace a multiple sets of characters with a single character

  4. 4

    How do I complete the Gnome Character Map with missing Unicode characters?

  5. 5

    How do I replace a single character in a string in constant time and using no additional space?

  6. 6

    How do I replace a single character in a string in constant time and using no additional space?

  7. 7

    How do I replace two or more instances of a character in an Excel string?

  8. 8

    How do I replace characters in every string in my list in java?

  9. 9

    How do I replace space characters in a string with "%20"?

  10. 10

    How do i search & replace using sed and not include a group of characters?

  11. 11

    How do I replace non word characters with a dash using TSQL?

  12. 12

    How do I replace a character in a string that repeats many times with a different character in python?

  13. 13

    How can I replace some characters with `'` character in a text file using sed?

  14. 14

    C#: Replace Certain Characters if they are the First Character of the String Using Regex

  15. 15

    How to replace character in the string using regex in java?

  16. 16

    How do I replace the " character in Java?

  17. 17

    How can I replace certain characters in a string?

  18. 18

    How to replace a character in C# string ignoring other characters?

  19. 19

    How do I replace illegal characters in a filename?

  20. 20

    How do I replace characters with a variable in CMD?

  21. 21

    How do I replace a comma to "\," in a string using sed

  22. 22

    How do I replace HTML content with a string in python using BeautifulSoup?

  23. 23

    How do I replace a character which is not preceded by \ by [character] globally?

  24. 24

    How do i Replace every n-th character x from an String

  25. 25

    How do I "unzoom" the currently selected character in Windows Character Map?

  26. 26

    How do I iterate through a map that has value, Map<String,Integer> declared as Map<String,Map<String,Integer>>using Expression Language ?

  27. 27

    How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

  28. 28

    How do I replace a backslash and newline character in sequence using regular expressions in Powershell?

  29. 29

    How can I match characters in a string and palce a dot after each Letter using preg replace

HotTag

Archive