Python script to generate words

2020-07-27 05:04发布

I'm looking to write a short script that will allow me to generate all possible letter combinations with the parameters I set.

For example:

__ _ _ n o

Parameters:

word = 5 letters

4th, 5th letter = n, o

1st letter = any vowel (aeiouy)

2nd, 3rd letter = any letter (abcde...)

in other words, I'm looking to write a script that would return me all 26*26*6 results. It does not matter if it is an actual word (i.e., "zzzno" is fine). And then to generalize it so I can do this with any parameters. Thank you.

标签: python
2条回答
一夜七次
2楼-- · 2020-07-27 06:02
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"))

returns 4056 entries:

['aaano', 'aabno', 'aacno', 'aadno', 'aaeno', 'aafno' ... ]
查看更多
我只想做你的唯一
3楼-- · 2020-07-27 06:03

If you have a word list, you don't even need to write code, you can use grep:

$ grep '^[aeiouy][a-z][a-z]no$' /usr/share/dict/words
amino
imino
查看更多
登录 后发表回答