在元音+周围辅音处拆分字符串

2024-03-29 14:37:59 发布

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

我对编码是新手,这是我的第一次尝试。我想从语音语言中将单词分成音节

用语音语言中的单词组成音节的规则:

<>考虑所有辅音直到第一元音,考虑元音。 重复一遍

例如:

ma-ri-a

a-le-ksa-nda-r

我已经走了这么远:

    word = 'aleksandar'
    vowels = ['a','e','i','o','u']
    consonants = ['b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z']

    for vowel in vowels:

        if vowels in word:

            index_1 = int(word.index(vowel)) - 1
            index_2 = int(word.index(vowel)) + 1

            print(word[index_1:index_2])

        else:

            print(consonants)

IDK出了什么问题,请帮忙! 提前感谢:)


Tags: in语言编码index语音单词wordint
2条回答

这应该能解决你的问题

word = 'aleksandar'
vowels = ['a','e','i','o','u']

new_word = ""

for letter in word:
    if letter in vowels:
        new_word += letter + "-"
    else:
        new_word += letter

print(new_word)

我已经改变了你的代码一点点,它的工作完美

word = 'aleksandar'
word = list(word)
vowels = ['a','e','i','o','u']
s = ""
syllables = [ ]
for i in range(len(word)):
    if word[i] not in vowels:
        s = s + word[i]
    else:
        s = s + word[i]
        syllables.append(s)
        s = ""
print(syllables)    

输出为:

['a', 'le', 'ksa', 'nda']

相关问题 更多 >