【问题标题】:Fix program that checks word for occurrences of 2 characters next to each other修复检查单词是否出现相邻的 2 个字符的程序
【发布时间】:2019-04-05 17:22:32
【问题描述】:

我正在尝试让我的程序检查字符串中是否存在字符串中相邻的列表中的 2 个字符的实例,并返回一个不同的字符串来替换这两个字符。

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 之前的字符是否是元音,但在正常工作之前不要担心

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    使用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
    

    【讨论】:

      【解决方案2】:

      我会重组你的代码,使其更加模块化:

      dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
      newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
      
      def dubbizer(word):
         for itter in range(len(dubs)):
             word = word.replace(dubs[itter], "-"+newdubs[itter])
         return word
      
      print(dubbizer("wanai"))
      

      这应该会给你wan-eye的输出

      没有替换:

      dubs = ["ai", "ae", "ao", "au", "ei", "eu", "iu", "oi", "ou", "ui"]
      newdubs = [ "eye", "eye", "ow", "ow", "ay","eh-oo", "ew", "oy", "ow","ooey"]
      
      def dubbizer(word):
         for itter in range(len(dubs)):
           while dubs[itter] in word:
             word = word[:word.find(dubs[itter])]+"-"+newdubs[itter]+word[word.find(dubs[itter])+len(dubs[itter]):]
         return word
      
      print(dubbizer("wanai"))
      

      【讨论】:

      • 还有没有其他方法可以做到这一点而无需替换()?谢谢
      • @Togoneforgood 我不明白你为什么想要那个,但我添加了那个。再次检查答案。
      猜你喜欢
      • 1970-01-01
      • 2016-06-27
      • 2021-09-18
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-26
      相关资源
      最近更新 更多