元音+周围辅音处的分裂字符串



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

从语音语言的单词中提取音节的规则:

考虑所有的辅音,直到第一个元音,考虑那个元音。重复

示例:

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出了什么问题,请帮忙!提前感谢:(

这应该可以解决问题

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

最新更新