简体   繁体   English

Python 用 lambda 删除一个字符

[英]Python remove a character with lambda

I want to remove “X” form this string.我想从这个字符串中删除“X”。 What is wrong with my code?我的代码有什么问题?

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message=filter(lambda x: x=="X" x=='' ,garbled)
print message

I know that this one works:我知道这个有效:

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message=filter(lambda x: x!="X" ,garbled)
print message

You have the mistake in lambda-condition.你在 lambda 条件下有错误。 You even do not need string.replace() .你甚至不需要string.replace() Lambda-condition must have this syntax: Lambda 条件必须具有以下语法:

Use the syntax lambda input: true_return if condition else false_return to return true_return if condition is True and false_return otherwise.使用语法 lambda 输入: true_return if condition else false_return 如果条件为 True 则返回 true_return,否则返回 false_return。 condition can be an expression involving input.条件可以是涉及输入的表达式。 ( There are more examples ) 还有更多例子

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
f = lambda x: "" if x in "X" else x
message = filter(f, garbled)
"".join(message)

filter() is a built-in function of Python. filter()是 Python 的内置 function。 The documentation says:文档说:

filter(function, iterable)

So you need two things to call the filter function: another function and basically a list of things.所以你需要两件事来调用过滤器 function:另一个 function 和基本上是一个列表。

For iterable , a string is fine, because a string is a list of characters.对于iterable ,字符串很好,因为字符串是字符列表。 That means, Python can perform a loop on it.这意味着,Python 可以对其执行循环。 That's what's important for an iterable.这对于可迭代对象来说很重要。

For function , let's read on:对于function ,让我们继续阅读:

Construct an iterator from those elements of iterable for which function returns true.根据 function 返回 true 的那些可迭代元素构造一个迭代器。 iterable may be either a sequence, a container which supports iteration, or an iterator. iterable 可以是序列、支持迭代的容器或迭代器。 If function is None, the identity function is assumed, that is, all elements of iterable that are false are removed.如果 function 为 None,则假定身份为 function,即移除所有为 false 的 iterable 元素。

So it says you need a function that returns True for those items that shall be iterated.所以它说你需要一个 function 来为那些需要迭代的项目返回True For all items you don't want, return False .对于您不想要的所有项目,返回False

Such a function could be:这样的 function 可以是:

def remove_all_X(s):
    if s == "X":
        return False  # because you want to remove all "X"
    return True  # all other characters

This can be inversed to这可以反转为

def remove_all_X(s):
    if s != "X":
        return True  # non-"X"
    return False  # "X"

And then shortened to然后缩短为

def remove_all_X(s):
    return s != "X"

You can then use it like然后你可以像这样使用它

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
message=filter(remove_all_X ,garbled)
print message

Note that there are no braces after remove_all_X , because you don't want to pass the result of the function, but the function itself.请注意,在remove_all_X之后没有大括号,因为您不想传递 function 的结果,而是传递 function 本身的结果。 Remember: filter() needs a function.请记住: filter()需要一个 function。

A function that is as simple as一个简单的 function

def remove_all_X(s):
    return s != "X"

is often expressed as a lambda.通常表示为 lambda。 You remove the definition ( def ) and write lambda + the parameter instead.您删除定义 ( def ) 并改写lambda + 参数。 The return keyword is also omitted. return关键字也被省略。 And you get:你得到:

labmda s: s != "X"

Now you use that instead of the function name:现在您使用它而不是 function 名称:

message=filter(labmda s: s != "X" ,garbled)

What is wrong with my code?我的代码有什么问题?

Let's try moving backwards in the explanation, but starting from your code让我们尝试在解释中向后移动,但从您的代码开始

lambda x: x=="X" x==''

Adding all the stuff that was removed:添加所有已删除的内容:

def remove_all_X(x):
    return x=="X" x==''

Do you see that this does not result in a valid return statement?您是否看到这不会导致有效的return语句? It looks like you want to return 2 things (maybe a tuple (x=="X", x=='') ) or a combination of two things (maybe x=="X" or x=="" )?看起来您想返回两件事(可能是一个元组(x=="X", x=='') )或两件事的组合(可能是x=="X" or x=="" )?

The compiler cannot interpret your wish and doesn't understand what to do.编译器无法解释您的愿望,也不知道该怎么做。

It's because lambda and filter just don't work like that.这是因为 lambda 和过滤器不能那样工作。 This is what happens when you try the first one当您尝试第一个时会发生这种情况

>>> message=filter(lambda x: x=="X" x=='', garbled)
SyntaxError: invalid syntax

The second one basically allows every letter where letter!='X' (so where every letter is not 'X')第二个基本上允许letter!='X'每个字母(所以每个字母都不是'X')

The Syntax Error appears because filter doesn't find every instance of a letter and replace it.出现语法错误是因为过滤器没有找到字母的每个实例并替换它。 It simply looks for a letter and excludes them, so putting the x=='' after the x=="X" doesn't make sense because there is an unexpected space, so Syntax Error ...它只是查找一个字母并将它们排除在外,因此将x==''放在x=="X"之后没有意义,因为存在意外空格,因此Syntax Error ...

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

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