Вот решение, которое может обрабатывать ввод текста со словами произвольной длины:
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])
Не нужно проверять каждую перестановку входных символов; оно совпадает, когда количество букв во входном слове совпадает с количеством букв во входном файле.
Это было мое первое решение, и оно работает, но не вводите строку со словом длиной более 10 символов, иначе ваш компьютер выйдет из строя:
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)