【问题标题】:im trying to convert my function into a lambda我试图将我的函数转换为 lambda
【发布时间】:2020-06-20 05:11:09
【问题描述】:

我想将此函数转换为 lambda,但我想知道我做错了什么。

temperatures=[10,20,30,11,22,33]

def check_weather(temp,low,high):
    if temp in range(low,high):
        return True
    else:
        return False

print(list(filter(lambda weather,low,high: True if weather in range(low,high) else False,temperatures)))

错误是:

TypeError: () 缺少 2 个必需的位置参数:'low' 和 'high'

【问题讨论】:

标签: python lambda


【解决方案1】:

您不需要 lambda。如果可能的话,您可以将函数用作对象

def check_weather(temp,low,high):
    return low <= temp <= high

print(list(filter(check_weather, weathers)))

但是过滤器函数只传递一个参数给内部过滤器函数

如果你想完成这个,你可以定义一个partial,但是不清楚你是如何分配低值和高值的

【讨论】:

    【解决方案2】:

    你必须先分配低值和高值

    【讨论】:

      猜你喜欢
      • 2019-04-24
      • 2016-03-02
      • 1970-01-01
      • 2017-01-05
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-24
      相关资源
      最近更新 更多