代码之家  ›  专栏  ›  技术社区  ›  shantanuo

提取连续使用元音符号的单词

  •  0
  • shantanuo  · 技术社区  · 2 年前

    我试图提取元音符号连续使用两次(或两次以上)的单词。

    texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
    signs = ['a', 'e', 'i', 'o', 'u']
    
    for i in texts:
        for x in i:
            if x in signs:
                print ("double vowel sign exists in", i)
    

    这将打印:

    double vowel sign exists in ane
    double vowel sign exists in ane
    double vowel sign exists in mood
    double vowel sign exists in mood
    double vowel sign exists in xao
    double vowel sign exists in xao
    double vowel sign exists in aa
    double vowel sign exists in aa
    

    预期输出为:

    double vowel sign exists in mood
    double vowel sign exists in mood
    double vowel sign exists in xao
    double vowel sign exists in xao
    double vowel sign exists in aa
    double vowel sign exists in aa
    

    (最好不要重复)

    4 回复  |  直到 2 年前
        1
  •  5
  •   Rahul K P no11    2 年前

    你可以这样做,

    texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
    signs = ['a', 'e', 'i', 'o', 'u']
    
    for i in texts:
        for x in zip(i, i[1:]):
            if all(check in signs for check in x):
                print(f"double vowel sign exists in {i}")
    

    输出

    double vowel sign exists in mood
    double vowel sign exists in xao
    double vowel sign exists in aa
    
        2
  •  2
  •   0x0fba    2 年前

    不是最简洁的,但可读性很强。 一旦找到双元音,就停止迭代一个单词。

    texts = ["ane", "mood", "xao", "pqr", "aa"]
    signs = ["a", "e", "i", "o", "u"]
    
    
    def has_two_consecutive_vowels(word: str) -> bool:
        counter = 0
        for letter in word:
            counter = counter + 1 if letter in signs else 0
            if counter > 1:
                return True
        return False
    
    
    for word in texts:
        if has_two_consecutive_vowels(word):
            print(f"double vowel sign exists in {word}")
    
        3
  •  1
  •   amanb    2 年前

    请尝试以下解决方案。我们在中的每个字符串上循环 texts 并将每个字符串拆分为一个字符列表,如 ['a', 'n', 'e'] 对于 "ane" 。然后你可以循环浏览这个列表,检查每个索引和下一个索引处的字符是否也是元音。我们将这些字符串附加到一个空列表中,然后可以打印它们。

    texts = ['ane', 'mood', 'xao', 'pqr', 'aa']
    signs = ['a', 'e', 'i', 'o', 'u']
    consecutive_vowels = []
    
    for i in texts:
        list_i = list(i)
        for j in range(len(list_i)-1):
            if (list_i[j] in signs) and (list_i[j + 1] in signs):
                consecutive_vowels.append(i)
                
    for word in consecutive_vowels:
        print("double vowel sign exists in {}".format(word))
    

    输出

    double vowel sign exists in mood
    double vowel sign exists in xao
    double vowel sign exists in aa
    
        4
  •  1
  •   OysterShucker    2 年前

    这是一个正则表达式版本。它只是检查双元音,如果找到了,就会保留单词。

    dbl = __import__('re').compile(r'[aeiou]{2}')
    txt = ('ane', 'mood', 'xao', 'pqr', 'aa')
    mtc = [w for w in txt if dbl.search(w)]
    print('double vowels in:', *mtc, sep='\n\t') 
    
        5
  •  0
  •   Raibek    2 年前

    您可以使用正则表达式来定义所需的模式。

    https://docs.python.org/3/library/re.html

    import re             
    pattern = ".*[" + ''.join(signs) + "]{2,}"
    for _text in texts:
        if re.search(pattern, _text):
            print(f"double vowel sign exists in {_text}")
            
    double vowel sign exists in mood
    double vowel sign exists in xao
    double vowel sign exists in aa