Thursday 15 July 2010

python - Why is my list comprehension causing the code to return my entire text document rather than only matches of words from permutations of a word? -



python - Why is my list comprehension causing the code to return my entire text document rather than only matches of words from permutations of a word? -

my code:

from itertools import permutations original = str(input('what word unscramble?: ')) inputfile = open('dic.txt', 'r') compare = inputfile.read().split('\n') inputfile.close() in permutations(original): auto = [print(now) in compare if in compare] #supposed compare iterations of input word text file.

i trying unscramble word finding permutations of word , running each permutation through text file of english language words see if real word or not. previous version stored permutations in list (now know that's bad idea). code here prints entire text file, , i'm not exclusively sure why. know i'm doing wrong list comprehension prints entire text file of words rather iterating through permutations of input word.

you can create code more python-idiomatic in multiple ways:

from itertools import permutations original = str(input('what word unscramble?: ')) open('dic.txt') input_file: compare = input_file.readlines() permutation in permutations(original): if permuation in compare: print(permutation)

does looking for?

python iteration permutation itertools

No comments:

Post a Comment