Python - Efficiently working with large permutated lists -


i've written short anagram solver script in python 2.7.

#import permutations module itertools import permutations perm  #defined functions def check(x,y):      #opens dictionary file     open('wordsen.txt') dictionary:          '''checks permutations against of dictionary words , appends         matching ones on of empty lists'''          line in dictionary:             in x:                 if + '\n' == line:                     y.append(i)  #empty lists appended later anagram_perms = [] possible_words = []  #anagram user input anagram = list(raw_input("input scrambled word: ").lower())  #creates single list items permutations, deletes duplicates char in perm(anagram):     anagram_perms.append("".join(char))     anagram_perms = list(set(anagram_perms))  #uses defined function check(anagram_perms, possible_words)  #prints number of perms created, prints possible words beneath print len(anagram_perms) print '\n'.join(possible_words) 

it takes user inputted anagram, generates , places list possible combinations of letters (using itertools.permutations), deleting duplicates. checks each of these of combinations against 100000 word dictionary text file, placing matching words list printed.

i've run issue if user inputs word above 6 unique letters in length, number of permutations generated causes hanging , crashes. 9 letter anagrams typical input, evidently these output 362880 ('9!') permutations if letters different, unfeasible.

i've though couple of potential solutions:

  1. creating number of empty lists can hold number of appended permuations. once these lists 'full', permutations added next one. each of these lists subsequently checked against text file.
  2. creating 1 empty list contained within loop. permutations generated , appened list workable number, list used check text file before emptying , appending in next number of permutations.
  3. some other method whereby number of permutations generated, process paused while generated ones checked against text file, , resumed , repeated.

i'm new python development , don't know if these possible or how go implementing them code; , other questions on similar topics haven't been able help.

if see code far i'd happy condense down , post it, sake of not making question longer i'll leave out unless it's requested. (updated above)

thanks!

it think best solution may not use permutations. it's more not generated permutations not word - it's waste generate them all.

you can consider pre-processing dictionary dictionary of sorted letters list of words letters consist of. then, anagram solver simple lookup in dictionary after sorting input.

first, create dictionary word list , save file:

from collections import defaultdict import json  word_list = ['tab', 'bat', 'cat', 'rat', ...]  # 100k words word_dict = defaultdict(list) word in word_list:     word_dict[''.join(sorted(word))].append(word) open('word_dict.json') f:     f.write(json.dumps(dict(word_dict))) 

then, when running anagram code, load dictionary , use sorted input:

import json  empty_list = [] open('word_dict.json', 'r') f:     word_dict = json.loads(f.read())  while true:     anagram = raw_input('enter in anagram: ')     sorted_anagram = ''.join(sorted(anagram))     print word_dict.get(sorted_anagram, empty_list) 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -