Script returns the same value multiple times

Basically I have a script in Python that takes multiple letters, gets each combination of them, and then checks if it is really a real word (think about how it works), but for some reason it returns the same words multiple once which I don't want to do, the script looks like this:

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 the way I meant it, only printing the 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 just like it does in every case it shows in the list, although as far as I can tell I am missing the same result that occurs multiple times in the check_combo routine, having got a list of got and a list of words

I have a feeling that the problem might come from the get_combos routine in the while loop, although I've tried modifying almost everything but didn't help, so I turn to those more knowledgeable than me for help.

+3


source to share


2 answers


        if a in got:
            got.append(a)
        else:
            got.append(a)
            combos.append(a)

      

This is almost certainly not what you meant :)

It seems that you want unique results from permutations. You make it too complicated and slow at the same time (because you are using it list

as a data structure for searching).

In particular, you need a result set , as in the mathematical concept of a collection of unique things. Luckily for you, Python has this built-in module.

Indeed, you are over-complicating the whole problem and your interface is wrong; you don't have to be print

results at the innermost level, but at the outermost (after the return

relevant data). Although you have more levels than you need, because you are doing too much work to process the lists of data manually. Just describe the data you want: the intersection of a set of words that you can make from tiles with words actually contained in the dictionary. The first is a set of letter-combining results from letter combinations that you get from multiple iterators itertools.permutations

that you can chain together with itertools.chain

.



def get_combos(letters):
    return set(
        ''.join(x)
        for x in itertools.chain(*(
            itertools.permutations(letters, count)
            for count in range(len(letters))
        ))
    ).intersection(english_words)

      

Done.

Or you can filter the set along the way:

def get_combos(letters):
    return set(
        ''.join(x)
        for x in itertools.chain(*(
            itertools.permutations(letters, count)
            for count in range(len(letters))
        ))
        if is_english_word(''.join(x))
    )

      

+2


source


This little sequence doesn't look right:

        if a in got:
            got.append(a)
        else:
            got.append(a)
            # ....

      



If you really want to add a

in got

no matter which branch is selected, do so outside the block if

.

0


source







All Articles