Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python在单词列表中显示元音_Python_List_Loops_Count - Fatal编程技术网

使用python在单词列表中显示元音

使用python在单词列表中显示元音,python,list,loops,count,Python,List,Loops,Count,我怎样才能得到一个像上面那样的列表并拥有python呢 检查列表中的每三个元素,并计算该字符串中的元音数 打印字符串及其包含的元音数 如果字符串包含3个或更多元音,则退出循环 要从世界列表中提取每三个元素,请使用: wordlist = ['dog', 'cat', 'mouse', 'alpaca', 'penguin', 'snail'] 这意味着,从索引0开始,通过列表将索引增加三 现在,要获得元音的数量,您有很多选择: 使用列表理解,可以执行以下操作: wordlist[0::3]

我怎样才能得到一个像上面那样的列表并拥有python呢

  • 检查列表中的每三个元素,并计算该字符串中的元音数
  • 打印字符串及其包含的元音数
  • 如果字符串包含3个或更多元音,则退出循环

  • 要从
    世界列表
    中提取每三个元素,请使用:

    wordlist = ['dog', 'cat', 'mouse', 'alpaca', 'penguin', 'snail']
    
    这意味着,从索引0开始,通过列表将索引增加三

    现在,要获得元音的数量,您有很多选择:

    • 使用列表理解,可以执行以下操作:

      wordlist[0::3]
      
    • 使用正则表达式并替换:

       vowels = 'aeiouy'
       vowels_only = [c for c in w.lower() if c in 'aeiou']
      
    总而言之,你会得到:

     import re
     re.sub(r'[aeiou]', '', w, flags=re.IGNORECASE)
    
    wordlist = ['dog', 'cat', 'mouse', 'alpaca', 'penguin', 'snail']
    
    for w in wordlist[0::3]:      
      vowels_only = [c for c in w.lower() if c in 'aeiou']
      nb_vowels = len(vowels_only) 
      print("%s (%d)" % (w, nb_vowels)) # print the word and vowels count
      if nb_vowels >= 3:
        break