我试图让我的程序检查字符串中相邻的两个字符的实例,并返回一个不同的字符串来替换这两个字符
def main():
dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
word = input("ENTER WORD : " )
count = 0
fin = []
while count < len(word):
if word[count:count+2] in dubs:
if word[count:count+2] == dubs[0]:
fin.append(newDubs[0] + "-")
if word[count:count+2] == dubs[1]:
fin.append(newDubs[1] + "-")
if word[count:count+2] == dubs[2]:
fin.append(newDubs[2] + "-")
if word[count:count+2] == dubs[3]:
fin.append(newDubs[3] + "-")
if word[count:count+2] == dubs[4]:
fin.append(newDubs[4] + "-")
if word[count:count+2] == dubs[5]:
fin.append(newDubs[5] + "-")
if word[count:count+2] == dubs[6]:
fin.append(newDubs[6] + "-")
if word[count:count+2] == dubs[7]:
fin.append(newDubs[7] + "-")
if word[count:count+2] == dubs[8]:
fin.append(newDubs[8] + "-")
if word[count:count+2] == dubs[9]:
fin.append(newDubs[9] + "-")
if word[count:count+2] not in dubs:
fin.append(word[count])
count+=1
fin= "".join(fin)
print(fin)
像wanai
这样的词,我期望wan-eye
结果是waneye-i
我还需要运行检查,看看dubs
前面的字符是否是元音,但在正常工作之前不要担心这个问题
我会将您的代码重组为更加模块化:
这将为您提供
wan-eye
的输出无需更换:
使用
zip()
+replace()
:相关问题 更多 >
编程相关推荐