So I have tried to make it so that I can extract words in a file with every English word from random letters a generator gives me. Then I would like to add the found words to a list. But I am having a bit of a problem acquiring this result. Could you help me please?
This is what I have tried:
import string import random def gen(): b = [] for i in range(100): a = random.choice(string.ascii_lowercase) b.append(a) with open('allEnglishWords.txt') as f: words = f.read().splitlines() joined = ''.join([str(elem) for elem in b]) if joined in words: print(joined) f.close() print(joined) gen()
if you are wondering where I got the txt file it is located here http://www.gwicks.net/dictionaries.htm. I downloaded the one labeled ENGLISH – 84,000 words the text file
Advertisement
Answer
import string import random b = [] for i in range(100): a = random.choice(string.ascii_lowercase) b.append(a) b = ''.join(b) with open('engmix.txt', 'r') as f: words = [x.replace('n', '') for x in f.readlines()] output=[] for word in words: if word in b: output.append(word) print(output)
Output:
['a', 'ad', 'am', 'an', 'ape', 'au', 'b', 'bi', 'bim', 'c', 'cb', 'd', 'e', 'ed', 'em', 'eo', 'f', 'fa', 'fy', 'g', 'gam', 'gem', 'go', 'gov', 'h', 'i', 'j', 'k', 'kg', 'ko', 'l', 'le', 'lei', 'm', 'mg', 'ml', 'mr', 'n', 'no', 'o', 'om', 'os', 'p', 'pe', 'pea', 'pew', 'q', 'ql', 'r', 's', 'si', 't', 'ta', 'tap', 'tape', 'te', 'u', 'uht', 'uk', 'v', 'w', 'wan', 'x', 'y', 'yo', 'yom', 'z', 'zed']