独立音节创建规则

2024-10-02 08:30:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python的初学者。到目前为止我得到了这个:

vowels = 'aeiouAEIUO'
consonants = 'bcdfghjklmnñpqrstvwxyzBCDFGHJKLMNÑPQRSTVWXYZ'
a = input('Type a word: ')

for i in range(len(a)):
    cont1 = 0
    cont2 = 0
    if a[i] in consonants:
        cont1 = i - 1
        cont2 = i + 1
        if a [cont1] in vowels and a [cont2] in vowels:
                print('consonant between vowels')

我想把音节分开。我想创建一个规则,当一个辅音在两个元音之间时,辅音必须连接到右边的元音,并打印由“-”分隔的音节 montoya=周一到周五 阿莫尔=阿莫尔 clase=类别

当然,这段代码会给出一个错误:IndexError:string index out of range。我不知道那是什么意思。你知道吗

谢谢


Tags: inifrange音节元音初学者辅音consonants
1条回答
网友
1楼 · 发布于 2024-10-02 08:30:41

I want to create a rule that when a consonant is between 2 vowels, the consonant has to join to the vowel on it's right.

您可以使用re

>>> import re
>>> vowels = 'aeiouAEIUO'
>>> consonants = 'bcdfghjklmnñpqrstvwxyzBCDFGHJKLMNÑPQRSTVWXYZ'
>>> pattern="([" + vowels + "])" + "([" + consonants + "]" + "[" + vowels + "])"
>>> re.sub(pattern, r'\1-\2', "amor")
'a-mor'
>>> re.sub(pattern, r'\1-\2', "clase")
'cla-se'
>>> 

相关问题 更多 >

    热门问题