1

So, I'm working on a python project (I'm a beginner), but I'm having trouble comparing a list to words in a text file. It's for a program that should unscramble words.

your_chars = input("Input characters:")
complete_list = []
final_lst = []
for current in range(len(your_chars)):
    a = [i for i in your_chars]
    for y in range(current):
        a = [x + i for i in your_chars for x in a]
    complete_list = complete_list+a
with open("P:/words.txt", "r") as file:
    for i in complete_list
        for x in file:
            final_lst.append(x)
print(final_lst)

I think it should work, but obviously it's not very efficient (especially the last three lines), but I can't think of another way to write it.

Ex:

input: yhe

output: hey

Any tips?

5
  • What exactly is the complete_list doing here? It looks like you repeat the lines a large number of times. Commented Aug 28, 2018 at 17:56
  • You're iterating through the text file twice, and you never close the input file. Try with open(..) as file and for line in file. Commented Aug 28, 2018 at 17:57
  • The last nested loop iterates on complete_liast, but doesn't use i. That just a purposeless repeat. Commented Aug 28, 2018 at 17:57
  • Can you add some sample data, input and expected output? Commented Aug 28, 2018 at 18:03
  • @AnkitJaiswal done Commented Aug 28, 2018 at 18:09

1 Answer 1

1

Here's a solution that can handle text-input with words of arbitrary length:

from collections import Counter

your_text = input('input characters')
with open('P:/words.txt', 'r') as infile:
    file_text = infile.read()

old_words = {
    str(sorted(Counter(w).items())): w
    for w in file_text.split()
}

for w in your_text.split():
    i = str(sorted(Counter(w).items()))
    if i in old_words:
        print(old_words[i])

It doesn't need to check each permutation of input characters; it matches when the count of letters in an input word is the same as one from your input file.


This was my 1st solution and works, but do not input a string with a word longer than 10 chars or it will crash your computer:

from itertools import permutations

perms_list = []
perms = []
matches = []
your_chars = input("input characters")
your_words = your_chars.split()

for word in your_words:
    perms_list.append([i for i in permutations(word)])
for word in perms_list:
    for perm in word:
        perms.append(''.join(list(perm)))

with open('P:/words.txt', 'r') as comparefile:
    file_contents = comparefile.read().split()
for permutation in perms:
    if permutation in file_contents:
        matches.append(permutation)
print(matches)
Sign up to request clarification or add additional context in comments.

5 Comments

the issue i was having is comparing the contents of the string complete_list with the words.txt file. If a word occurred in both, they will be added to final_list, which will be printed to the console.
if P:/words.txt is really big, this might not be the most efficient way to go about it.
@BenjaminFasick - Tested it and working for me. Lmk if it is for you.
This is actually kind of an interesting problem. Going to write a better solution which won't crash python if you type in a super-long string, and that can handle a massive file.
Works for me! It seems like a pretty cool way to handle it, definitely exposed me to some new concepts, so thanks so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.