我需要打破一个字符串。 子字符串必须组成:
示例: 字符串:“ dipestaggio” 结果:['di','pe','sta','ggio']
字符串:“ odiattuazio” 结果:['o','dia','ttua','zio']
我尝试使用此代码,但这是错误的:
def separate(word):
A = word[0]
L = []
for pre,cur in zip(word[2:], word[1:]):
if cur in voc:
A += cur
elif cur not in voc:
L.append(A)
if pre in voc: A = ''
A += cur
return L
有人说的话很强吗? 谢谢
答案 0 :(得分:1)
根据我们到目前为止的信息,这可能是一种解决方案:
string = "dipestaggio"
voc = ["a", "e", "i", "o", "u"]
def separate(word):
A = ""
L = []
for letter in word:
if len(A) < 1:
A += letter
else:
if A[len(A) - 1] not in voc:
A += letter
else:
if letter in voc:
A += letter
else:
L.append(A)
A = letter
L.append(A)
return L
print(separate(string))
dipestaggio
的输出为:
['di', 'pe', 'sta', 'ggio']
更新:如果要将辅音串联成元音(如果辅音是第一个字母),可以将if len(A) < 1
更改为if len(A) <= 1