【问题标题】:How to detect vowels in a string and if a specific letter is beside a vowel, it is also considered a string? [duplicate]如何检测字符串中的元音,如果一个特定的字母在元音旁边,它也被认为是一个字符串? [复制]
【发布时间】:2019-11-28 20:24:14
【问题描述】:

使用一个函数来检测给定字符串中是否有元音,同时检查字母“g”是否在元音旁边。如果字母“g”在元音旁边,那么它也将被视为元音。我确实发布了一个与此类似的问题并得到了一个几乎可行的答案,但我没有得到关于它是如何完成的解释,也没有人回复我要求澄清的评论。

函数如下:

    import re


def disemvowel(text):
    result = re.sub(r"G[AEIOU]+|[AEIOU]+G|[AEIOU]+", "", text, flags=re.IGNORECASE)
    print(result)


disemvowel("fragrance")
# frrnc

disemvowel('gargden')
# rgdn

disemvowel('gargdenag')
# rgdn

此功能适用于大多数情况,除了字母“g”在元音之前和超过元音时。例如,当我输入“gag”时它不起作用,当它不应该返回任何东西时它返回“g”。我只需要澄清一下这个函数是如何工作的,以及我可以对其进行哪些编辑以使其在所有情况下都能正常运行。

这是我最初使用的函数,但它只适用于元音,因为我不知道如何添加一个条件来检测元音旁边的字母“g”:

def disemvowel(text):
text = list(text)
new_letters = []
for i in text:
    if i.lower() == "a" or i.lower() == "e" or i.lower() == "i" or i.lower() == "o" or i.lower() == "u":
        pass
    else:
        new_letters.append(i)
print (''.join(new_letters))

disemvowel('fragrance')
# frgrnc

【问题讨论】:

    标签: python-3.x for-loop if-statement while-loop


    【解决方案1】:

    两种方法。

    import re
    def methodA(txt,ii):
    
        vowels = ['a', 'e', 'i', 'o', 'u']
        txt = re.sub('g'+ vowels[ii] +'g' ,' ', txt) # remove strings like gug
        txt = re.sub(vowels[ii] + 'g', ' ', txt) # remove strings like ug
        txt = re.sub('g' + vowels[ii] , ' ', txt) # remove strings like gu
    
        if (ii == len(vowels)-1 or txt == '' or txt == ' ') : # if string is empty or all vowels have been used
            txt = "".join( list(filter(lambda x:x not in ['a', 'e', 'i', 'o', 'u'], list(txt)))) # finally remove all vowels
            txt = re.sub(' ', '', txt)
            return txt
        ii = ii + 1
        return methodA(txt, ii) # call the function with next vowel in the list
    

    ans = methodA("fragrance",0) # 用第零个元音初始化

    frrnc

    from itertools import permutations
    def methodB(txt):
        vowels = ['a', 'e', 'i', 'o', 'u'] # vowels to remove
        a = [] # store permutation of strings to remove in this list
        for vowel in vowels: # create the list of all combo of characters that need to be removed
            a.extend(list(map(lambda x : x[0]+x[1]+x[2]  , list(permutations('g' + vowel + 'g')) )) )
            a.extend(list(map(lambda x : x[0]+x[1]  , list(permutations('g' + vowel )) )))
    
        lims = list(set([re.sub('gg','', xx)  for xx in a ])) # we don't need gg and the duplicates
        lims.sort(key = len,reverse = True) # create a list of strings sorted by length
        for ll in lims:
            txt = re.sub(ll, ' ', txt) # remove the required strings with largest ones first
        return re.sub(' ','',txt)
    

    ans=methodB("香水")

    frrnc

    【讨论】:

      【解决方案2】:

      以下是此任务的解决方案:

      def disemvowel(text):
          return re.sub(r"G?[AEIOU]+G?", "", text, flags=re.IGNORECASE)
      
      
      tests = {"fragrance": "frrnc", "gargden": "rgdn", "gargdenag": "rgdn", "gag": ""}
      
      for test, value in tests.items():
          assert disemvowel(test) == value
      
      print("PASSED")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-15
        • 2012-09-05
        • 2019-06-25
        • 1970-01-01
        • 2019-10-10
        • 2021-12-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多