简体   繁体   English

Python元音过滤器,附加到字符串

[英]Python vowel filter, appending to string

I've had a look around and seen a million and one questions like this, but none quite how I am trying to do this, so please be gentle! 我已经环顾四周,看到了一百万个这样的问题,但没有一个我想要做到这一点,所以请温柔!

Basically I'm trying to add the vowels in a sentence to a list??? 基本上我是想把一个句子中的元音添加到列表中??? and print the list, all this by using a lambda called isVowel, and a function called vowelFilter. 并使用名为isVowel的lambda和名为vowelFilter的函数打印列表。

I am really unsure how to do this using a lambda, and what I have done so far doesn't seem to work, here's my code; 我真的不确定如何使用lambda做到这一点,到目前为止我所做的似乎不起作用,这是我的代码;

sentence = "The quick brown fox jumps over the bridge"
vowels = "aeiouAEIOU"
finalVowel = []
def vowelFilter(sentence):
        for letter in sentence:
                if letter in vowels:
                        finalVowel.append(letter)
        return finalVowel

Can you guys shed any light on this? 你们能否对此有所了解? Again I'm well aware this has been asked 1000000000x times but nothing that is 100% like mine, so please be kind! 再次,我很清楚这已被问过1000000000倍,但没有100%像我一样,所以请善待!

Thanks 谢谢

You can use a list comprehension with a condition. 您可以将列表理解与条件一起使用。 There is no need for a lambda: 不需要lambda:

>>> l = [v for v in sentence if v in vowels]
>>> print l
['e', 'u', 'i', 'o', 'o', 'u', 'o', 'e', 'e', 'i', 'e']

Just 只是

finalVowel = [x for x in sentence if x in vowels]

I hope this can help you 我希望这可以帮到你

Using a lambda expression here is not very pythonic, but if you must: 在这里使用lambda表达式不是非常pythonic,但如果你必须:

sentence = "The quick brown fox jumps over the bridge"
vowels = "aeiouAEIOU"
finalVowel = []

# Return True if arg is in vowels; otherwise, return False
isVowel = lambda arg: arg in vowels

def vowelFilter(sentence):
    for letter in sentence:
        if isVowel(letter):  # Call the lambda to test if letter is a vowel
            finalVowel.append(letter)
    return finalVowel

print (vowelFilter(sentence))

From the docs : 来自文档

lambda_expr ::= "lambda" [parameter_list]: expression

old_lambda_expr ::= "lambda" [parameter_list]: old_expression

Lambda expressions (sometimes called lambda forms) have the same syntactic position as expressions. Lambda表达式(有时称为lambda表单)与表达式具有相同的句法位置。 They are a shorthand to create anonymous functions; 它们是创建匿名函数的简写; the expression lambda arguments: expression yields a function object. 表达式lambda arguments:expression产生一个函数对象。 The unnamed object behaves like a function object defined with 未命名的对象的行为类似于使用定义的函数对象

def name(arguments): return expression

See section Function definitions for the syntax of parameter lists. 有关参数列表的语法,请参阅函数定义一节。 Note that functions created with lambda expressions cannot contain statements. 请注意,使用lambda表达式创建的函数不能包含语句。

If it's a must to use lambda then you can do it like this (Which is not different than the other two answers) 如果必须使用lambda那么你就可以这样做(这与其他两个答案没有什么不同)

vowel_filter = lambda sent: [x for x in sent if x in vowels]
vowel_filter(sentence)
>>> ['e', 'u', 'i', 'o', 'o', 'u', 'o', 'e', 'e', 'i', 'e']

finalVowel = vowel_filter(sentence)
print finalVowel
>>> ['e', 'u', 'i', 'o', 'o', 'u', 'o', 'e', 'e', 'i', 'e']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM