python - 修复检查单词是否出现相邻的 2 个字符的程序
问题描述
我试图让我的程序检查字符串中是否存在字符串中相邻的列表中的 2 个字符的实例,并返回一个不同的字符串来替换这两个字符。
定义主():
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
是否是元音,但在正常工作之前不要担心
解决方案
使用zip()
+ replace()
:
dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
s = 'wanai'
for x, y in zip(dubs, newdubs):
s = s.replace(x, f'-{y}')
print(s)
# wan-eye
推荐阅读
- android - 如何使用后退按钮取消选择gridview中的所有项目?
- javascript - HTML 文件似乎忽略了 javascript 文件
- firebase - FCM 代币是否被重复使用?
- javascript - 在角度 5 中验证同一字段中的电子邮件和电话号码
- python - 如何将 lxml.etree._XSLTResultTree 转换为 python 中的元组?
- php - 如何在php中安装作曲家以进行条带支付?
- java - Gradle - 复制多模块项目构建中所有更改模块的 jar
- sql - 如何设置Bit类型输入参数可选设置默认为false
- c - C: 如何访问存储在 void 指针 (void *) 中的函数指针?
- javascript - 如何检查 youtube 中的视频是否直播或使用 youtube iframe api 上传