I'm trying to port the code from coding for crosswords from C++ to Python.
I have a dictionary where the keys are 12000 english words and the values for each key is a list of possible combinations of the key with missing letter. For example, the key-value pairs for "DOG" and "DAD" are:
my_dic = {
"DOG": ["DOG", "DO-", "D-G", "D--", "-OG", "--G", "-O-", "---"],
"DAD": ["DAD", "DA-", "D-D", "D--", "-AD", "--D", "-A-", "---"],
...
}
I'm trying to create a sort of "hash table" for which, given a value, it will return all possible keys that will match. For example, if I ask for "D--", the function will return "DOG", and "DAD".
I can flatten all values and add the key to a list using a list comprehension, but is it the fastest method?
def find_word(s):
my_keys = []
tmp = [k,v for sublist in list(my_dic.values() for k,v in sublist]
if s in tmp:
my_keys.append(k)
return my_keys