python中的嵌套lambda表达式map和reduce
我的代码当前包含,作为 while 循环条件的一部分:
reduce(operator.or_, map(lambda y: reduce(operator.or_, map(lambda x: x[0] == y, data[ testoffset:])), footers))
它的目的是检查 python array.array 实例的给定切片是否包含几个特定字节值之一。
我收到的错误是:
NameError:全局名称'y'未定义
所以我很确定这是一个范围问题。但我想不出一种方法可以从这里做我想做的事。
My code currently contains, as part of the condition for a while loop:
reduce(operator.or_, map(lambda y: reduce(operator.or_, map(lambda x: x[0] == y, data[testedoffset:])), footers))
It's purpose is to check if a given slice of a python array.array instance contains one of several specific byte values.
The error I'm getting is:
NameError: global name 'y' is not defined
So I'm pretty sure it's a scoping issue. But I can't think of a way to do what I want from here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我看到你自己找到了答案,但是当你在这里时......
该代码确实需要一些工作。
我不完全确定为什么要在
footers
序列中基于data[testedoffset:]
映射该表达式。这似乎没有任何效果,除非您的 __getitem__ 有副作用。但整个
map+reduce+operator.or_
的事情让我感到不舒服。尝试更多类似这样的事情:
I see you found the answer on your own, but while you're here...
That code really could use some work.
I'm not entirely sure why you're mapping that expression based on
data[testedoffset:]
across thefooters
sequence. That doesn't seem to have any effect whatsoever, unless your__getitem__
has side effects.But the whole
map + reduce + operator.or_
thing gives me the willies.Try something more like this:
这肯定不是范围问题,而且它显然是一个非Pythonic表达式。
这是我尝试理解它的方法,我发现您必须将 y 传递给 lambda 表达式。
That sure is not a scoping issue and it is clearly an unpythonic expression.
Here is my attempt to understand it and I find that you have to passing y to the lambda expression.