PeddiePooh
PeddiePooh

Reputation: 403

Python - change text in string by random from a list

I want to write a loop function that go through each letter in my list called original.

original = ['ABCD', 'DCBA', 'AAAA', 'AABB']
letters  = ['A', 'B', 'C', 'D'] 

p = 1

for o in original: # loop through the original list
    for i in range(0,len(o)): # loop through each letter in selected list

        if random.randint(1,10) == p: #if this gives me the probability that is met
        # I want to change the current letter on the current index to 
        # something else different from the letter list by random (maybe random.choice)

Im new to python please can you advice. I dont want to use class or any other library but random please

Upvotes: 0

Views: 234

Answers (3)

DarrylG
DarrylG

Reputation: 17166

Assuming you want to update original

import random

original = ['ABCD', 'DCBA', 'AAAA', 'AABB']
letters  = ['A', 'B', 'C', 'D'] 

p = 1

for i, o in enumerate(original):
    new_letters = []     # updated letters for word o 
    for c in o:
      if random.randint(1,10) == p:
        t = letters[:]   # copy of letters
        t.remove(c)      # remove letter from copy (so letters remains unchanged)
        new_letters.append(random.choice(t))  # choice over remaining letters
      else:
        new_letters.append(c)

    original[i] = ''.join(new_letters)        # convert newsletters list to string 
                                              # and replace in original

print(original)

Upvotes: 1

Nathaniel D. Hoffman
Nathaniel D. Hoffman

Reputation: 295

First, the zero in

for i in range(0, len(o))

is redundant. You want to give random.choice a list of letters that include everything in letters minus the current letter. The fastest way I can think of doing this is with a set:

newletters = list(set(letters).difference(o[i])

Now you have a list that includes all the letters in "letters" except for the letter at o[i].

To assign the letter (after you get it from random.choice), turn your "original" word into a list:

o_list = list(o)

and assign it as

l = random.choice(newletters)
o_list[i] = l
new_word = "".join(o_list)

As for actually inserting that new word back into your list of originals, you would have to know the index of the old word - I would use enumerate to do this:

original = ['ABCD', 'DCBA', 'AAAA', 'AABB']
letters  = ['A', 'B', 'C', 'D'] 

p = 1
for index, o in enumerate(original): # loop through the original list
    for i in range(len(o)): # loop through each letter in selected list
        if random.randint(1,10) == p:
            newletters = list(set(letters).difference(o[i])
            o_list = list(o)
            l = random.choice(newletters)
            o_list[i] = l
            new_word = "".join(o_list)
            original[index] = new_word

Upvotes: 1

user10852642
user10852642

Reputation:

In python, you can not modify strings at all. You can get letters by index, select specific strings, but not modify them. To change the said list you can use original.pop(o) and add the said edited string in the list with original.append('AB" + random.choice(letters) + 'C' as you said. To be more clear: you use list.append(element) to add element to list and you use list.pop(element) to remove element from list. Again, you can never edit strings in python, you can only create new ones and store the edited old ones, for example, new_string = old_string[:4], this particular code will store all the characters in old_string, up to index 4 into the new string. Really hope I helped!

Upvotes: 1

Related Questions