''' 在华为二面的时候,部门大佬问了我一个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对象才能输出
python中的map和lambda
最新推荐文章于 2024-04-28 11:35:41 发布