python - 在元音+周围辅音处拆分字符串
问题描述
我是编码新手,这是我的第一次尝试。我想将单词从语音语言中分成音节。
用语音语言中的单词组成音节的规则:
考虑所有辅音,直到第一个元音,考虑那个元音。重复。
例子:
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出了什么问题,请帮忙!提前致谢 :)
解决方案
我已经稍微更改了您的代码,并且效果很好!
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']
推荐阅读
- android - 二进制 XML 文件行:与 androidx.navigation.fragment.NavHostFragment 的另一个片段重复的 id 、标记 null 或父 id
- java - 使用具有不同字节数组参数的参数捕获器验证多个方法调用在 getAllValues() 的所有实例中返回相同的值
- protractor - 通过单击量角器中的下一步/保存按钮,选项卡导航不起作用
- python - python pandas read_excel没有捕捉到异常
- amazon-s3 - terraform s3 远程状态文件自动加密
- python - 如何使用 networkx 获得中心性度量?nx.betweenness_centrality 似乎不起作用
- python - 比较日期并添加一列与以前的交易日期
- django - 如何实现django Guardian来限制用户访问模型实例
- reactjs - 错误:重新渲染过多。React 限制渲染次数以防止无限循环,当我清除存储的会话数据时它会消失
- c# - 我应该在将 .p7s 注入 Pdf 之前或之后何时添加 Ltv?