我想将此函数转换为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个必需的位置参数:“低”和“高”
答案 0 :(得分:1)
您不需要lambda。如果可能的话,可以将该函数用作对象
def check_weather(temp,low,high):
return low <= temp <= high
print(list(filter(check_weather, weathers)))
但是,过滤器功能仅将一个参数传递给内部过滤器功能
如果要完成此操作,可以定义一个partial
,但是不清楚如何分配低值和高值
答案 1 :(得分:0)
您必须先分配低值和高值