我有以下列表。我想用这个patern,元音+辅音+元音+辅音列出所有可能的单词
consonants=["b","c","k"]
vowels=["a","o"]
word1=vowels,consonants,vowels,consonants
我使用了以下代码,但它不遵循模式
def generate (word):
import itertools
for i in itertools.permutations(consonants):
for j in itertools.permutations( vowels):
print (''.join(i),''.join(j))
答案 0 :(得分:3)
from itertools import product
from pprint import pprint
pprint([''.join(t) for t in product(vowels, consonants, vowels, consonants)])
会给:
['abab',
'abac',
'abak',
'abob',
'aboc',
'abok',
'acab',
'acac',
'acak',
'acob',
'acoc',
'acok',
'akab',
'akac',
'akak',
'akob',
'akoc',
'akok',
'obab',
'obac',
'obak',
'obob',
'oboc',
'obok',
'ocab',
'ocac',
'ocak',
'ocob',
'ococ',
'ocok',
'okab',
'okac',
'okak',
'okob',
'okoc',
'okok']
答案 1 :(得分:0)
[v1 + c1 + v2 + c2 for v1 in vowels for c1 in consonants for v2 in vowels for c2 in consonants]
请注意确定是否有更好的方式。
编辑:Elmex80s的答案是更好的方式。