I'm trying to create a list of words that contains all possible combinations of character replacement by changing multiple characters with their respective lists. The input is also a list of keywords. Example:
keywords=["magic", "mate"]
aoptions = ["a", "4", "@"]
boptions = ["b", "8"]
eoptions = ["e", "3"]
goptions = ["g", "9"]
ioptions = ["i", "1"]
loptions = ["l", "1"]
ooptions = ["o", "0"]
soptions = ["s", "5", "$"]
toptions = ["t", "7"]
zoptions = ["z", "2"]
The desired result would be a list like this:
['magic', 'mag1c', 'ma9ic', 'ma91c'...'m@t3', 'm@73']
I've only been able to create a solution for one keyword at a time and substituting a single character with another single character. The algorithm was found here String Replacement Combinations. It looks like this
from itertools import product
def filler(word, from_char, to_char):
options = [(c,) if c != from_char else (from_char, to_char) for c in word]
return (''.join(o) for o in product(*options))
Which results in:
>>> filler("magic", "a", "4")
<generator object <genexpr> at 0x8fa798c>
>>> list(filler("magic", "a", "4"))
['magic', 'm4gic']
It isn't particularly important, but the list of keywords will be read from a .txt file with a single keyword on each line and the resulting list of combinations will be written to a .txt file with a single word on each line. I've been trying to create different iterative loops and modifying the itertools.product example for days without any luck. Any and all help is much appreciated.
UPDATE: Updating my filler function based on #zefciu advice I was able to solve using this method:
from itertools import product
def filler(word):
combos = [(c,) if c not in options else options[c] for c in word]
return (''.join(o) for o in product(*combos))
options = {
'A': ['A', '4', '@'],
'B': ['B', '8',],
'E': ["E", "3"],
'G': ["G", "9"],
'I': ["I", "1", "!"],
'L': ["L", "1"],
'O': ["O", "0"],
'S': ["S", "5", "$"],
'T': ["T", "7"],
'Z': ["Z", "2"]}
with open('CustomList.txt', 'r') as f:
startlist = f.readlines()
startlist = [x.strip() for x in startlist]
startlist = [element.upper() for element in startlist]
filid= open('WordList.txt', 'w+')
for word in startlist:
temp_list=list(filler(word))
for newword in temp_list:
print >> filid, newword
filid.close()