How to do mutation with word in python?
Question: I'm tryin to play with words. For example, I read word "script" from a txt
file. Then would like to do all mutations of letters and write each of them.
So here are changes

    
    
     s=$
     s=5
     s=S
     s=s
     c=(
     c=[
     c={
     c=<
     c=c
     c=C
     r=r
     r=R
     i=i
     i=I
     i=|
     i=1
     i=!
     .
     .
     .
    

What i was thinking to get is

    
    
     scrypt
     $crypt
     5cript
     Scrypt
     s(ript
     $(cript
     .
     .
     .
    

So all possible combinations. Similar to itertool.product I'm little confused
how to do that. I started like this

    
    
     def main():
    
        with open('file1.txt', 'r') as f1, open('file2.txt', 'w') as f2:
           for word in f1:
               l=len(word)
               for i in range(l):
                   if word[i] = s:
                       word1=word[i].raplace("$") #don't know if sintacs is ok
                       f2.write(word1)
                   else: 
                       if word[i] = c:
                    .
                    .
    

Now I'm confused here. I would have to ask for every letter in alphabet and
I'm complicating this.

I would probably need to have lots of loops inside loop. I think how many
characters in word thats how manu for loops.

  

ITs to complicated and probably to slow. Is there some easy way? Some function
(tool) to import?

Question is how to deal with to same letter in one word and how t

P.S I'm using python 3.4.2


Answer: Construct a dictionary that maps each letter to its permitted replacements.
Then use `itertools.product` to find all the combinations of those
replacements.

    
    
    import string
    import itertools
    
    replacements = """
    s=$
    s=5
    s=S
    c=(
    c=[
    c={
    c=<
    c=C
    r=R
    i=I
    i=|
    i=1
    i=!
    """
    
    
    d = {c:[c] for c in string.printable}
    for line in replacements.strip().split("\n"):
        c, replacement = line.split("=")
        d[c].append(replacement)
    
    word = "script"
    for letters in itertools.product(*[d[c] for c in word]):
        print("".join(letters))
    

Result:

    
    
    script
    scrIpt
    scr|pt
    scr1pt
    ...
    S(R1pt
    S(R!pt
    S[ript
    S[rIpt
    ...
    SCRIpt
    SCR|pt
    SCR1pt
    SCR!pt
    

