im试图将我的函数转换为lambda

问题描述 投票:-1回答:2

我想将此函数转换为lambda,但是idk我在做什么错。

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
2个回答
1
投票

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

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

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

但是,过滤器功能仅将一个参数传递给内部过滤器功能

如果您想完成此操作,则可以定义partial,但是尚不清楚如何分配低值和高值


0
投票

您必须先分配低和高值

© www.soinside.com 2019 - 2024. All rights reserved.