python - python中的嵌套lambda表达式map和reduce

标签 python arrays dictionary slice reduce

我的代码当前包含,作为 while 循环条件的一部分:

reduce(operator.or_,map(lambda y:reduce(operator.or_,map(lambda x:x[0]==y,data[testedoffset:])),页脚))

它的目的是检查 python array.array 实例的给定切片是否包含几个特定字节值之一。

我收到的错误是:

NameError:全局名称“y”未定义

所以我很确定这是一个范围问题。但我想不出一种方法可以从这里做我想做的事。

最佳答案

我看到你自己找到了答案,但是当你在这里时...... 该代码确实需要一些工作。

我不完全确定为什么您要根据 data[testedoffset:] 映射该表达式跨越footers顺序。这似乎没有任何影响,除非你的 __getitem__有副作用。

但是整个map + reduce + operator.or_这件事让我心潮澎湃。

尝试更多类似这样的事情:

y = 'whatever'
if any(x[0] == y for x in data[offset:]):
    print "yep, it's in there"

关于python - python中的嵌套lambda表达式map和reduce,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5086357/

相关文章:

python - Pandas json_normalize 当存在空值时将整数转换为 float

javascript - 使用Underscore获取满足特定条件的元素的所有索引

arrays - Angular : Get last 3 elements of json object

arrays - Swift 数组初始化 : data[section][row]

python - 按日期排序字典列表

python - 循环中断而不执行最后一个打印语句

python - SPARQL:查询链接到一个 URL 但不链接到另一个 URL 的所有三元组

python - 安装 pygrib 包

arrays - VBA Excel 计算特定值

python - 从函数迭代字典