Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Randomly Replacing Characters in String with Character Other than the Current Character

Suppose that I have a string that I would like to modify at random with a defined set of options from another string. First, I created my original string and the potential replacement characters:

string1 = "abcabcabc"
replacement_chars = "abc"

Then I found this function on a forum that will randomly replace n characters:

def randomlyChangeNChar(word, value):

length = len(word)
word = list(word)
# This will select the distinct index for us to replace
k = random.sample(range(0, length), value) 
for index in k:
    # This will replace the characters at the specified index with the generated characters
    word[index] = random.choice(replacement_chars)
# Finally print the string in the modified format.
return "".join(word)

Then a second string can be defined by calling this function (I arbitrarily set this as n = 4 in this example since this number comes from another variable in my actual code):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

string2 = randomlyChangeNChar(string1, 4)
print(string2)

By turning these outputs into lists, they can be compared for differences easily with:

print("Modified Base Pairs with Indices:\n")
l_string1 = list(string1)
l_string2 = list(string2)
for index, (first, second) in enumerate(zip(l_string1, l_string2)):
        if first != second:
            print(index, first, second)

This code does what I want with one exception — it does not account for characters in string1 that match the random replacement character. I understand that the problem is in the function that I am trying to adapt, I predict under the for loop, but I am unsure what to add to prevent the substituting character from equaling the old character from string1. All advice appreciated, if I’m overcomplicating things please educate me!

>Solution :

In the function you retrieved, replacing:

word[index] = random.choice(replacement_chars)

with

word[index] = random.choice(replacement_chars.replace(word[index],'')

will do the job. It simply replaces word[index] (the char you want to replace) with an empty string in the replacement_chars string, effectively removing it from the replacement characters.

Another approach, that will predictably be less efficient on average, is to redraw until you get a different character from the original one:

that is, replacing:

word[index] = random.choice(replacement_chars)

with

char = word[index]
while char == word[index]:
    char = random.choice(replacement_chars)
word[index] = char

or

while True:
    char = random.choice(replacement_chars)
    if char != word[index]:
        word[index] = char
        break
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading