简体   繁体   English

Python辅音/字元排列

[英]Python Consonant/Vowel Permutations

I want to generate all possible permutations for specific consonant/vowel setups. 我想为特定的辅音/元音设置生成所有可能的排列。 For example, I would like all possible 3 letter word permutations with the format of CVC (Consonant, Vowel, Consonant). 例如,我希望所有可能的3个字母的单词排列都采用CVC格式(辅音,元音,辅音)。

I don't know of any way to multiply permutations: 我不知道增加排列的任何方法:

permutations("bcdfghjklmnpqrstvwxyz",1) * permutations("aeiou",1) * permutations("bcdfghjklmnpqrstvwxyz",1)

How can this be achieved in python? 如何在python中实现?

itertools.product to the rescue: itertools.product的解救:

>>> import itertools
>>> consonants = "bcdfghjklmnpqrstvwxyz"
>>> vowels = "aeiou"
>>> poss = list(itertools.product(consonants, vowels, consonants))
>>> len(poss)
2205
>>> poss[:10]
[('b', 'a', 'b'), ('b', 'a', 'c'), ('b', 'a', 'd'), ('b', 'a', 'f'), ('b', 'a', 'g'), ('b', 'a', 'h'), ('b', 'a', 'j'), ('b', 'a', 'k'), ('b', 'a', 'l'), ('b', 'a', 'm')]

or if you want words specifically: 或者,如果您想要特定的单词:

>>> words = list(''.join(letters) for letters in itertools.product(consonants, vowels, consonants))
>>> words[:10]
['bab', 'bac', 'bad', 'baf', 'bag', 'bah', 'baj', 'bak', 'bal', 'bam']

Here's a non-itertools based solution: 这是一个非基于itertools的解决方案:

>>> import string
>>> vowels = set('aeiou')
>>> consonants = set(string.ascii_lowercase) - vowels
>>> cvc_generator = (''.join((c1, v, c2)) for c1 in consonants for v in vowels for c2 in consonants) 
>>> cvc_generator.next()
'cac'
>>> cvc_generator.next()
'cab'
>>> cvc_generator.next()
'cad'
>>> cvc_generator.next()
'cag'
>>> print ' '.join(cvc for cvc in cvc_generator)
caf cah cak caj cam cal can caq cap cas car cat caw cav cay cax caz cic cib cid cig cif cih cik cij cim cil cin ciq cip cis cir cit ciw civ ciy cix ciz cec ceb ced ceg cef ceh cek cej cem cel cen ceq cep ces cer cet cew cev cey cex cez cuc cub cud cug cuf cuh cuk cuj cum cul cun cuq cup cus cur cut cuw cuv cuy cux cuz coc cob  <..snip..>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python中使用正则表达式来查找遵循模式的单词:元音,辅音,元音,辅音 - Regex in Python to find words that follow pattern: vowel, consonant, vowel, consonant 在python中将随机字符串重新排列为辅音元音 - rearrange random string to consonant vowel consonant vowel order in python 如何在Python中重复发出辅音,但不发元音 - How to get consonant repeated, but not vowel in Python 评估 Python 中单词串的辅音/元音组成 - Evaluate consonant/vowel composition of word string in Python python 正则表达式计算英语元音/辅音比率 - python regex to calculate vowel/consonant ratio in English 如何使用Python简单地添加组合元音每个辅音 - How to simple add combination vowel each consonant using Python 如何检查弦是否由辅音,元音和辅音组成? - How to check if string consist of consonant, vowel and consonant? 用元音和辅音模式列出所有可能的单词 - Make a list of all possible word with vowel and consonant pattern 创建一个使用元音 = 0 辅音 = 1 将二进制替换为单词的函数 - creating a function to sobstitute binary to words using vowel=0 consonant=1 给定一个字符串,检查以元音开头的子串的数量是否大于以辅音开头的子串的数量 - Given a string, check if the number of substrings starting with a vowel greater than with a consonant
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM