python - 在Python中返回按元音计数过滤的列表的函数

标签 python string list

我需要编写一个函数,通过按元音计数过滤列表来返回列表。

我尝试了这个,但输出不正确:

def filter_by_vowel_count(input, count):

    for words in input:
        for p in words:
            if p in 'aeiou':
                value +=1
                if value == count:
                    list1.append(words)

最佳答案

使用sum和生成器表达式:

>>> def filter_by_vowel_count(words, count):
...     result = []
...     for word in words:
...         if sum(p in 'aeiou' for p in word) == count:
...             result.append(word)
...     return result
...
>>> fruits = ['banana', 'apple', 'lemon', 'pineapple', 'coconut']
>>> filter_by_vowel_count(fruits, 2)
['apple', 'lemon']
>>> filter_by_vowel_count(fruits, 3)
['banana', 'coconut']

关于python - 在Python中返回按元音计数过滤的列表的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351278/

相关文章:

c# - 简单的查找功能

python - 将列表的字符串表示形式转换为带引号和双引号的列表

python - 从架构中的每个表中获取所有单列

python - 使用 max() 函数查找所有具有最大长度的字符串

C:如何从套接字上的 read() 清除缓冲区

C 字符串数组

java - 计算整数数组元素的平方根时的 Arrays.asList 问题

python - PySpark reduceByKey 只用一个键

python - flask .cli.NoAppException : Failed to find application in module?

c - 从文件中获取数据并在每次新行开始时打印出来