0

这个程序将生成一个字母组合列表并检查它们是否是英文单词,但是程序会遗漏一些单词,我检查了字典文件,单词在那里但仍然没有在输出中,请告诉我为什么程序是遗漏了很多结果home corn barn,诸如此类。

import itertools
#http://www.puzzlers.org/pub/wordlists/engwords.txt

with open('/Users/kyle/Documents/english words.txt') as word_file:
    english_words = set(word.strip().lower() for word in word_file if len(word.strip()) == 4)

for p1 in itertools.combinations('abcdefghijklmnopqrstuvwxyz', 4):
    word = ''.join(p1)
    if word in english_words:
        print '{} is {}'.format(word, word in english_words)

这是我正在使用的字典http://www.puzzlers.org/pub/wordlists/engwords.txt

4

1 回答 1

1

您正在寻找product笛卡尔积),而不是combinations(不会产生字母不按字母顺序排列或有重复字母的单词):

import string

for letters in itertools.product(string.ascii_lowercase, repeat=4):
    word = ''.join(letters)

    if word in english_words:
        print word

您也可以使用元组而不是字符串,这样可以加快速度:

import string
from itertools import product
#http://www.puzzlers.org/pub/wordlists/engwords.txt

with open('/Users/kyle/Documents/english words.txt', 'r') as word_file:
    english_words = set(tuple(word.strip().lower()) for word in word_file if len(word.strip()) == 4)

words = english_words.intersection(product(string.ascii_lowercase, repeat=4))
于 2013-06-16T17:28:57.730 回答