元音和周围辅音的分割字符串

问题描述 投票:0回答:2

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

用注音语言的单词制成音节的规则:

请考虑所有辅音,直到第一个元音,再考虑该元音。重复。

示例:

m a-r i-a

a-l e-ks a-nd a-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出了什么问题,请帮忙!在此先感谢:)

python string split slice
2个回答
0
投票

我对您的代码进行了少许更改,并且效果很好!

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

0
投票

这应该可以解决您的问题

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)
© www.soinside.com 2019 - 2024. All rights reserved.