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:
JavaScript
21
1
import string
2
import random
3
4
def gen():
5
b = []
6
for i in range(100):
7
a = random.choice(string.ascii_lowercase)
8
b.append(a)
9
10
11
with open('allEnglishWords.txt') as f:
12
words = f.read().splitlines()
13
joined = ''.join([str(elem) for elem in b])
14
if joined in words:
15
print(joined)
16
f.close()
17
18
print(joined)
19
20
gen()
21
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
JavaScript
18
1
import string
2
import random
3
4
b = []
5
for i in range(100):
6
a = random.choice(string.ascii_lowercase)
7
b.append(a)
8
b = ''.join(b)
9
10
with open('engmix.txt', 'r') as f:
11
words = [x.replace('n', '') for x in f.readlines()]
12
13
output=[]
14
for word in words:
15
if word in b:
16
output.append(word)
17
print(output)
18
Output:
JavaScript
7
1
['a', 'ad', 'am', 'an', 'ape', 'au', 'b', 'bi', 'bim', 'c', 'cb', 'd', 'e',
2
'ed', 'em', 'eo', 'f', 'fa', 'fy', 'g', 'gam', 'gem', 'go', 'gov', 'h',
3
'i', 'j', 'k', 'kg', 'ko', 'l', 'le', 'lei', 'm', 'mg', 'ml', 'mr', 'n',
4
'no', 'o', 'om', 'os', 'p', 'pe', 'pea', 'pew', 'q', 'ql', 'r', 's', 'si',
5
't', 'ta', 'tap', 'tape', 'te', 'u', 'uht', 'uk', 'v', 'w', 'wan', 'x', 'y',
6
'yo', 'yom', 'z', 'zed']
7