【问题标题】:What does `map(lambda s: s != None, some_list)` mean? [duplicate]`map(lambda s: s != None, some_list)` 是什么意思? [复制]
【发布时间】:2023-02-20 22:43:23
【问题描述】:

我正在尝试使用 this tutorial 理解标准 Cart-pole 示例的深度 Q 学习算法,并且在 def optimize_model() 方法中,我不明白 lambda 表达式是返回布尔值还是索引:

non_final_mask = torch.tensor(tuple(map(lambda s: s is not None, batch.next_state)), device=device, dtype=torch.bool) 

其中 batch.next_state 只是一个列表,而 s 仅在此行中定义。

从文档和this example来看,lambda s: s is not None产生了一个布尔值。但是,当我简单地输入 python 时:

>>> lambda s: s is None

我明白了

<function <lambda> at 0x100997010>

如果我确实从上述 lambda 表达式中得到了一个布尔值,map() 方法如何将它作为第一个参数处理?

感谢您提前提供帮助。

【问题讨论】:

  • 函数是一个对象。如果你想称呼它,您需要添加括号并提供参数。 map 为您做这件事。
  • my_totally_cool_lambda = lambda s: s is None .... my_totally_cool_lambda ("Hi") .... my_totally_cool_lambda (None)
  • map 有据可查——它将一个函数作为第一个参数,将一个可迭代对象作为第二个参数——lambda s: s is not None 定义了一个使用 1 个参数并返回 True 或 False 的函数。 --> how-are-lambdas-useful
  • @Patrick Artner 谢谢!好的,那么在这种情况下 lambda 确实返回了一个布尔值。但是抱歉,我找不到 map() 如何将布尔值作为第一个参数处理,这就是我发布此问题的原因。
  • @trincot 谢谢!那么, map() 是否本质上执行了这个操作:(lambda s: s != None)(batch.next_state)

标签: python lambda


【解决方案1】:

它不仅与 lambda 表达式有关,还与 map 函数有关。 Lambda 只是一个函数,实际上可以返回任何类型。

is_x_none_lambda = lambda x: x is None # Funtion is declared AND assigned

def is_x_none_function(x)->bool:
    return x is None

some_other_name = is_x_none_function

# In REPL if you don't invoke them:
is_x_none_function # <function is_x_none_function at 0x7f81b467ab60>
is_x_none_lambda # <function <lambda> at 0x7f81b467aac0>

a = None
b = "String, not a None..."

# The return type of both of them is hovever "bool" indeed
# Again in REPL if you invoke any of the above:
is_x_none_function(a) # True
is_x_none_lambda(a) # True

is_x_none_function(b) # False
is_x_none_lambda(b) # False

# map
test_data = [None, 5, "test str"]
# All the calls below do the same
tuple(map(is_x_none_function, test_data))
tuple(map(some_other_name, test_data)) 
tuple(map(lambda x: x is None, test_data))
tuple(map(is_x_none_lambda, test_data)) 
# Take function which I provide (which can be a function assigned to a variable OR a lambda) and invoke it on each element of the "test_data"
# The "tuple" is needed as well, cause without it, you'd get <map object at 0x7f81b4643190>

旁注:

  • 而不是tuple,它可以是例如listmap 转换为数据结构

【讨论】:

    【解决方案2】:

    map 是一种类型,但对于第一个近似值,您可以假设它是一个定义为的函数

    def map(f, xs):
        for x in xs:
            yield f(x)
    

    第二次近似,它可以采取多种的可迭代的参数。在这种形式中,它使用其函数参数将可迭代对象“压缩”在一起,而不仅仅是产生元组。

    def map(f, *args):
        for t in zip(*args):
            yield f(*t)
    

    在您的示例中,该函数作为 lambda 表达式提供,而不是使用 def 语句定义的函数。

    non_final_mask = torch.tensor(tuple(map(lambda s: s is not None, batch.next_state)), device=device, dtype=torch.bool) 
    

    相当于

    def not_none(s):
        return s is not None
    
    non_final_mask = torch.tensor(tuple(map(not_none, batch.next_state)), device=device, dtype=torch.bool) 
    

    在任何一种情况下,map 都会产生一系列布尔值,这些值是通过对可迭代的batch.next_state 产生的每个元素应用一个函数而创建的;这些布尔值用于创建一个 tuple 作为第一个参数传递给 torch.tensor


    请注意map曾是Python 2 中的一个函数,它返回一个列表。

    >>> map(lambda s: s is not None, [1, None, 'c', None])
    [True, False, True, False]
    

    在 Python 3 中,它变成了一个类型,这样它就可以返回一个惰性可迭代对象。 (该函数仅在您迭代 map 的实例时应用,而不是在调用 map 时立即应用。)如果不存在 map 函数,Python 3 不太可能添加它(或者至少,它只能通过 itertools 模块使用,而不是作为内置类型使用)。 map 实际上总是可以替换为生成器表达式

    # map(f, xs)
    (f(x) for x in xs)
    

    【讨论】:

      猜你喜欢
      • 2019-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2021-05-27
      • 2022-06-10
      • 2021-11-02
      • 2011-09-27
      相关资源
      最近更新 更多