用于生成单词的Python脚本

时间:2011-04-12 01:23:12

标签: python

我正在编写一个简短的脚本,允许我使用我设置的参数生成所有可能的字母组合。

例如:

__ _ _ n o

参数:

word = 5个字母

第4,第5个字母= n,o

第一个字母=任何元音(aeiouy)

第2,第3个字母=任何字母(abcde ...)

换句话说,我正在编写一个脚本,它会返回所有26 * 26 * 6的结果。如果它是一个实际的单词并不重要(即“zzzno”很好)。然后概括它,以便我可以使用任何参数执行此操作。谢谢。

2 个答案:

答案 0 :(得分:5)

import itertools
import string

letter = string.lowercase
vowel  = "aeiouy"

def all_words(*args):
    return (''.join(letters) for letters in itertools.product(*args))

wordlist = list(all_words(vowel, letter, letter, "n", "o"))

返回4056个条目:

['aaano', 'aabno', 'aacno', 'aadno', 'aaeno', 'aafno' ... ]

答案 1 :(得分:1)

如果你有一个单词列表,你甚至不需要编写代码,你可以使用grep:

$ grep '^[aeiouy][a-z][a-z]no$' /usr/share/dict/words
amino
imino