简体   繁体   English

python中的lambda和过滤器

[英]lambda and filter in python

I have the following string and I wanna decode by filtering the X's我有以下字符串,我想通过过滤 X 来解码

garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"

And I would like to filter.我想过滤。 I tried the following code我尝试了以下代码

message = filter(lambda x: garbled.remove(x) if x == "X", garbled)

I did not make this way work.我没有让这种方式工作。 I have found this other solution:我找到了另一个解决方案:

message = filter(lambda x: x != "X", garbled)

But I still wonder why did not work the first one.但我仍然想知道为什么第一个不起作用。 Can I fix it?我可以修吗?

(I am new in python btw) thanks! (顺便说一句,我是 python 新手)谢谢!

You can use a generator expression instead of filter您可以使用生成器表达式而不是filter

>>> ''.join(i for i in garbled if i != 'X')
'I am another secret message!'

If you wanted to use filter you'd have to change your lambda to如果您想使用filter ,则必须将lambda更改为

>>> ''.join(filter(lambda x: x != 'X', garbled))
'I am another secret message!'

Cory already fixed your filter . Cory 已经修复了您的filter

In my opinion, your problem is handled best by a simple str.replace .在我看来,您的问题最好通过简单的str.replace处理。

>>> garbled = "IXXX aXXmX aXXXnXoXXXXXtXhXeXXXXrX sXXXXeXcXXXrXeXt mXXeXsXXXsXaXXXXXXgXeX!XX"
>>> garbled.replace('X', '')
'I am another secret message!'
message = filter(lambda x: garbled.remove(x) if x == "X", garbled)

This code doesn't work, because lambda function must be expression.这段代码不起作用,因为 lambda 函数必须是表达式。

garbled.remove(x) if x == "X"

This is statement, not a expression.这是陈述,不是表达。 I delete if x=="X" to make an valid expression,我删除 if x=="X" 以生成有效表达式,

message = filter(lambda x: garbled.remove(x), garbled)

Next, I catch the error "'str' object has no attribute 'remove'".接下来,我发现错误“'str' 对象没有属性 'remove'”。 Because type of garbled is string, and there is no attribute that named 'remove' in string type.因为乱码的类型是字符串,而字符串类型中没有命名为'remove'的属性。 To use filter function, first argument must be 'predicate' function (that return True or False), and second argument must be iterable-like.要使用过滤器函数,第一个参数必须是“谓词”函数(返回 True 或 False),第二个参数必须是类似可迭代的。 OP's second solution satisfies this condition. OP的第二个解决方案满足这个条件。 second argument is string that is iterable (a list of characters), and first argument is a predicate function that takes one character that is supplied from the second argument string)第二个参数是可迭代的字符串(字符列表),第一个参数是一个谓词函数,它采用第二个参数字符串提供的一个字符)

filter(function, iterable) does the following procedure: filter(function, iterable)执行以下过程:

  1. Go through the elements in iterable (ie the second parameter) one at a time.一次一个iterable的元素(即第二个参数)。
  2. For each one of those elements, call function (ie the second parameter) on that element and see whether it returns true.对于这些元素中的每一个,在该元素上调用function (即第二个参数)并查看它是否返回 true。
  3. Collect together only those elements where the function returned true, and return a new list of those.只收集那些function返回 true 的元素,并返回这些元素的新列表。

Look at what happens in step 2 if you pass lambda x: garbled.remove(x) if x == "X" for the function parameter: filter() says to itself, "Hmm, if I set x="I" then is garbled.remove(x) if x == "X" true?".看看第 2 步中如果你传递lambda x: garbled.remove(x) if x == "X"会发生什么lambda x: garbled.remove(x) if x == "X" for the function parameter: filter()对自己说,“Hmm, if I set x="I" then是garbled.remove(x) if x == "X" true?"。 It's like you're shouting an instruction at filter() in its second parameter: "hey, please remove all the "X" s".这就像您在filter()的第二个参数中大喊指令:“嘿,请删除所有的"X" s”。 But that's just not the right thing to go there.但这不是去那里的正确做法。 The requirements are very precise: it must be a function that takes an element and returns a true or false value.要求非常精确:它必须是一个接受元素并返回 true 或 false 值的函数。

You can do it this way.你可以这样做。 You have a mistake in lambda-condition.您在 lambda 条件中存在错误。 You even do not need string.replace() .你甚至不需要string.replace()

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

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

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