【发布时间】:2016-07-25 23:31:20
【问题描述】:
Celsius = [66.5,45.2,33.5,55.5]
Fahrenheit = [((float(9)/5)*x + 32) for x in Celsius]
如何在 lambda 函数中编写它?例如:λx,y:x+y
【问题讨论】:
标签: python list lambda list-comprehension
Celsius = [66.5,45.2,33.5,55.5]
Fahrenheit = [((float(9)/5)*x + 32) for x in Celsius]
如何在 lambda 函数中编写它?例如:λx,y:x+y
【问题讨论】:
标签: python list lambda list-comprehension
你是这个意思吗?
Fahrenheit = list(map(lambda x: x * 9.0 / 5 + 32, Celsius))
一般来说,列表理解(您的示例所做的)可以转换为 map 和 lambda(或其他函数)的组合。
编辑
你也可以使用lambda x: (float(9)/5)*x + 32;我只是想简化表达。 :-)
【讨论】:
TempCtoF = lambda c: 9/5 * c + 32
TempFtoC = lambda f: 5/9 * (f - 32)
Celsius = [66.5,45.2,33.5,55.5]
Fahrenheit = [TempCtoF(c) for c in Celsius]
或
Fahrenheit = list(map(TempCtoF, Celsius))
【讨论】: