Dani
Dani

Reputation: 41

Make a list of all possible word with vowel and consonant pattern

I have following list. And I want to make a list of all possible word with a this patern, vowels+consonants+vowels+consonants

consonants=["b","c","k"]
vowels=["a","o"]
word1=vowels,consonants,vowels,consonants

I have used following code but it does not follow the pattern

def generate (word):
      import itertools 
      for i in itertools.permutations(consonants):
            for j in itertools.permutations( vowels):
                  print (''.join(i),''.join(j))

Upvotes: 0

Views: 288

Answers (2)

Denziloe
Denziloe

Reputation: 8132

[v1 + c1 + v2 + c2 for v1 in vowels for c1 in consonants for v2 in vowels for c2 in consonants]

Note sure if there's a nicer way.

Edit: Elmex80s's answer is the nicer way.

Upvotes: 0

Elmex80s
Elmex80s

Reputation: 3504

from itertools import product
from pprint import pprint

pprint([''.join(t) for t in product(vowels, consonants, vowels, consonants)])

Will give:

['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']

Upvotes: 3

Related Questions