First I'm not sure what this is called formally but whatever.
Basically I need to form "wildcard buckets". I think the best way to explain what I need to do is to take an example. For instance, say I have the three words "aaa", "aab", and "baa".
Let me use *
as the wildcard. Then from aaa
I can form -aa
, a-a
, aa-
, --a
, a--
, a-a
, and ---
. I then add these to a dictionary where the keys are the buckets and values are sets of (complete) words that fit these buckets. I repeat for all my words. So in the end I get that, for instance, -a-
fits all three of the example words that I have above.
I have working code, but it is very slow in forming these buckets when I have a word list that has hundreds of thousands of words.
for numberSpaces in range(0, len(word) + 1): # loop through all spaces of word
buckets = itertools.combinations(range(0, len(word)), numberSpaces)
for indicesToReplace in buckets: # for each index in this combination
newWord = word
for index in indicesToReplace: # replace character with "*"
newWord = newWord[0:index] + "*" + newWord[index + 1:]
if newWord not in bucketDictionary.keys(): # Add to dictionary
bucketDictionary[newWord] = set()
bucketDictionary[newWord].add(word)
I would like to know how to best optimize this code. I've tried recursion but I'm not sure if that is any better than this iterative method.
Thank you!