Basically I have a script in Python that takes several letters, gets every combination of them and then checks to see if it’s an actual word (Think scrabble in a way) but for some reason it returns the same words multiple times which I don’t want it to do, the script looks like:
with open("dictionary.txt") as word_file:
english_words = set(word.strip().lower() for word in word_file)
def is_english_word(word):
return word.lower() in english_words
print is_english_word("ham")
print is_english_word("zz")
a = raw_input("Please enter first letter: ")
b = raw_input("Please enter second letter: ")
c = raw_input("Please enter third letter: ")
d = raw_input("Please enter fourth letter: ")
e = raw_input("Please enter fifth letter: ")
check =[a,b,c,d,e]
def get_combos(list):
import itertools
count = len(list)
got = []
combos =[]
while count > 0:
for a in itertools.permutations(list,count):
if a in got:
got.append(a)
else:
got.append(a)
combos.append(a)
count = count - 1
for a in combos:
strip_combos(a)
def strip_combos(list):
count = ''
words = []
for entry in list:
count = count + entry
words.append(count)
check_combo(words)
def check_combo(list):
words = []
got = []
for entry in list:
if is_english_word(entry):
if entry not in words:
print entry
words.append(entry)
get_combos(check)
Now it works as I meant it too by only printing words that are in the dictionary but it will print the same word many times for example if the letters are:
a, c, e, s
It will return as on every occasion it shows in the list though as far as I can tell I’m omitting the same result occurring many times in the check_combo procedure by having a got and a words list
I have a feeling the issue may stem from the get_combos procedure in the while loop somewhere though I’ve tried modifying pretty much everything to no avail so I’m turning to those more knowledgeable than myself for help.
This is almost certainly not what you meant 🙂
It seems that what you want to do is get the unique results from the permutations. You are making this much too complicated, and slower at the same time (because you’re using a
listas a data structure for lookup).Specifically, you want the set of results, as in the mathematical concept of a collection of unique things. Fortunately for you, Python has this built-in.
Really you’re making the whole problem too complicated, though, and your interface is wrong; you shouldn’t be
printing results at the innermost level, but at the outermost (afterreturning the appropriate data). Although you have more levels than you need, because you are doing too much work to process lists of data manually. Just describe the data you want: the intersection of the set of “words” you can make from the tiles, with the words actually in the dictionary. The former is the set of results of joining up letters from letter-combinations that you get from severalitertools.permutationsiterators, which you can string together withitertools.chain.Done.
Or you can filter the set as you go: