python中的map和lambda

85 篇文章 0 订阅
'''
在华为二面的时候,部门大佬问了我一个python的基础问题,
使用map和lambda将一个月的温度数据从摄氏度单位转换成华氏温度。
首先将一个月的温度数据存储到list列表中
map的作用是:对于iterable对象中的元素进行element wise的same operation
lambda的作用是:将函数匿名,即只给出本质的数学运算公式,不需要额外地定义函数。使用lamebda创建匿名函数
假设温度单位转换的公式就是取平方
http://www.runoob.com/python/python-func-map.html
'''
input_temp=[1,2,3,4,5]
def square(temperature):
    '''
    :param temperature: 这里所需要输入的温度是scaler标量数据
    :return: 返回值也是标量数据
    '''
    return temperature**2
output1=list(map(square,input_temp))
print(output1)
output2=list(map(lambda x:x**2,input_temp))
print(output2)
assert output2==output1
# map函数返回值是一个迭代器,需要强制转换成list对象才能输出
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值