Most Efficient Way to Replace Multiple Characters in a String

Malonge

Let's say there is a string of any length, and it only contains the letters A through D:

s1 = 'ACDCADBCDBABDCBDAACDCADCDAB'

What is the most efficient/fastest way to replace every 'B' with an 'C' and every 'C' with a 'B'.

Heres what I am doing now:

replacedString = ''
for i in s1:
    if i == 'B':
        replacedString += 'C'
    elif i == 'C':
        replacedString += 'B'
    else:
        replacedString += i

This works but it is obviously not very elegant. The probelm is that I am dealing with strings that can be ones of milliions of characters long, so I need a better solution.

I can't think of a way to do this with the .replace() method. This suggests that maybe a regular expression is the way to go. Is that applicable here as well? If so what is a suitable regular expression? Is there an even faster way?

Thank you.

Adam Smith

Apart from the str.translate method, you could simply build a translation dict and run it yourself.

s1 = 'ACDCADBCDBABDCBDAACDCADCDAB'

def str_translate_method(s1):
    try:
        translationdict = str.maketrans("BC","CB")
    except AttributeError: # python2
        import string
        translationdict = string.maketrans("BC","CB")
    result = s1.translate(translationdict)
    return result

def dict_method(s1):
    from, to = "BC", "CB"
    translationdict = dict(zip(from, to))
    result = ' '.join([translationdict.get(c, c) for c in s1])
    return result

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replace multiple (special) characters - most efficient way?

What is an efficient way to replace many characters in a string?

most efficient way to check if a string contains specific characters

Most efficient way to remove special characters from string

What is the most efficient way to find and replace continuing duplicates in the string?

Best way to replace multiple characters in a string?

How to replace particular characters of a string with the elements of a list in an efficient way?

Most efficient way to remove multiple substrings from string?

Java Replacing multiple different substring in a string at once (or in the most efficient way)

What is an easier way to replace multiple characters with other characters in a string in swift?

Most computationally efficient way to remove last word from string if it's less than x number of characters?

Most efficient way to strip forbidden characters in file name from Unicode string

Replace multiple characters in a string

Replace Multiple Characters in string

Most efficient way to pass multiple arguments to a function?

Most efficient way to recode these multiple if statements

Most efficient way to query a database multiple times

Most efficient way to compare multiple files in python

What is the most efficient way to work with multiple subelements?

Most efficient way for handling multiple files

Most efficient way to design an employee with multiple employers

Check if number is multiple of 5 in most efficient way

most efficient way to search for a word in multiple files

For input string with multiple words - what is the most efficient way to check if any of them start with some other string?

Most efficient way to keep collection of string references

Most efficient way of converting String to Integer in java

Most efficient way to check if object is a String

What is the most efficient way to convert an int to a String?

Most efficient way to extract a substring of a string in PHP