Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7781593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:16:38+00:00 2026-06-01T19:16:38+00:00

Basically I have a script in Python that takes several letters, gets every combination

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:16:41+00:00Added an answer on June 1, 2026 at 7:16 pm
            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 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 list as 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 (after returning 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 several itertools.permutations iterators, which you can string 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 as you go:

    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))
        )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well basically I have this script that takes a long time to execute and
So basically I have a PHP script that takes all the images from the
Basically, I have a script that cannot see an existing directory on the OS
Basically I have a script in Python that grabs the text from an open
I have a python script that analyzes a set of error messages and checks
I have a small python script which i use everyday......it basically reads a file
I have a script that I'm trying to get working. Basically, what I'm trying
I am stuck with a peculiar issue here. I have a script that basically
I have a simple script that does some search and replace. This is basically
I have this bash script on the server that runs every hour, via cron.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.